2011-05-16 113 views
0

我不明白返回类型...我是一名VB开发人员。它是否返回一些数组?返回类型描述

[System.Web.Services.WebMethod] 
[System.Web.Script.Services.ScriptMethod] 
public static object GetUploadStatus() 
{ 
    //Get the length of the file on disk and divide that by the length of the stream 
    UploadDetail info = (UploadDetail)HttpContext.Current.Session["UploadDetail"]; 
    if (info != null && info.IsReady) 
    { 
     int soFar = info.UploadedLength; 
     int total = info.ContentLength; 
     int percentComplete = (int)Math.Ceiling((double)soFar/(double)total * 100); 
     string message = "Uploading..."; 
     string fileName = string.Format("{0}", info.FileName); 
     string downloadBytes = string.Format("{0} of {1} Bytes", soFar, total); 
     return new { 
       percentComplete = percentComplete, 
       message = message, 
       fileName = fileName, 
       downloadBytes = downloadBytes}; 
    } 
    //Not ready yet 
    return null; 
} 

谢谢

+1

http://msdn.microsoft.com/en-us/library/bb397696.aspx - 匿名类型 – 2011-05-16 13:05:12

+0

尝试的http://转换器.telerik.com /获得初步翻译(在这种情况下翻译看起来不错) – 2011-05-16 13:10:50

+0

感谢@Bala它的一个好工具.. – 2011-05-17 04:34:58

回答

3

它返回一个anonymous type(VB.NET参考)。这是一种没有相应类的类型。

Visual Basic支持匿名类型,它允许您在不为数据类型编写类定义的情况下创建对象。相反,编译器会为您生成一个类。该类没有可用的名称,直接从Object继承,并且包含您在声明对象时指定的属性。由于数据类型的名称未指定,因此它被称为匿名类型。

3

没有其返回一个匿名类型。

3

您要退回Anonymous Type

它基本上就像在飞行中创建一个类一样。

方程式标记左侧的每个值都是属性名称。

1

这是返回一个具有以下属性的匿名类型(不是数组):percentComplete,message,fileName和downloadBytes。

0

看起来它正在返回一个带有属性percentComplete,message,fileName和downloadBytes的匿名实例。调用者将不得不使用反射来访问属性(或在.NET 4中使用dynamic关键字)。

更容易的是用这些属性创建一个类并返回该类型的实例而不是匿名类型以避免使用反射。

+2

其实你可以使用“Cast By例如“如果对象正在被读取的同一个程序集中*读取匿名类型的实例,并避免反射或动态。但是,如果您在同一个装配体内部进行此操作,那么为什么不只是制造一个内部标称类型呢? – 2011-05-16 14:14:24

0

它返回一个具有一些命名属性的对象。则返回null //Not ready yet

1

转换为VB可以帮助你:

<System.Web.Services.WebMethod> _ 
<System.Web.Script.Services.ScriptMethod> _ 
Public Shared Function GetUploadStatus() As Object 
    Dim info As UploadDetail = DirectCast(HttpContext.Current.Session("UploadDetail"), UploadDetail) 
    If info IsNot Nothing AndAlso info.IsReady Then 
     Dim soFar As Integer = info.UploadedLength 
     Dim total As Integer = info.ContentLength 
     Dim percentComplete As Integer = CInt(Math.Ceiling(CDbl(soFar)/CDbl(total) * 100)) 
     Dim message As String = "Uploading..." 
     Dim fileName As String = String.Format("{0}", info.FileName) 
     Dim downloadBytes As String = String.Format("{0} of {1} Bytes", soFar, total) 
     Return New With { _ 
      Key .percentComplete = percentComplete, _ 
      Key .message = message, _ 
      Key .fileName = fileName, _ 
      Key .downloadBytes = downloadBytes _ 
     } 
    End If 
    Return Nothing 
End Function 
+0

不太可能会帮助不知道匿名初始化程序的人。即使在VB.NET中,他们也会使用某种奇怪的花括号语法...... :-) – 2011-05-16 13:30:25