开发者

Description of return type

I do not understand the return type... I am a VB developer.开发者_JAVA百科 is it returning some array???

[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;
}

thank you


It is returning an anonymous type (VB.NET reference). It is a type that has no corresponding class.

Visual Basic supports anonymous types, which enable you to create objects without writing a class definition for the data type. Instead, the compiler generates a class for you. The class has no usable name, inherits directly from Object, and contains the properties you specify in declaring the object. Because the name of the data type is not specified, it is referred to as an anonymous type.


No its returning an anonymous type.


You're returning an Anonymous Type.

It's basically like creating a class on the fly.

Every value on the left side of an equation-mark is a property-name.


That's returning an anonymous type (not an array) with the following properties: percentComplete, message, fileName, and downloadBytes.


converted to VB might help you:

<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


Looks like it's returning an anonymous instance with attributes percentComplete, message, fileName and downloadBytes . The caller would have to use reflection to access the properties (or use dynamic keyword in .NET 4).

An easier thing to would be to create a class with these properties and return an instance of that type rather than the anonymous type to avoid using reflection.


It's returning an object with some named properties. Or null if //Not ready yet

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜