C# 退货类型说明
我不明白退货类型。。。我是一个VB开发人员。它是否返回了一些数组C# 退货类型说明,c#,C#,我不明白退货类型。。。我是一个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
[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;
}
谢谢否,它返回的是匿名类型。它返回的是(VB.NET引用)。它是一个没有相应类的类型 Visual Basic支持匿名类型,这使您可以创建对象,而无需为数据类型编写类定义。相反,编译器会为您生成一个类。该类没有可用的名称,直接从对象继承,并且包含在声明对象时指定的属性。由于未指定数据类型的名称,因此将其称为匿名类型 你在退回一张支票 这基本上就像是动态创建一个类
等式标记左侧的每个值都是一个属性名。返回的匿名类型(不是数组)具有以下属性:percentComplete、message、fileName和downloadBytes。看起来它返回的匿名实例具有percentComplete、message、fileName和downloadBytes属性。调用方必须使用反射来访问属性(或者在.NET4中使用
dynamic
关键字)
更简单的方法是创建一个具有这些属性的类,并返回该类型的实例,而不是匿名类型,以避免使用反射。它返回一个具有一些命名属性的对象。如果
//尚未准备好
转换为VB,则为null可能会帮助您:
<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
_
_
作为对象的公共共享函数GetUploadStatus()
作为UploadDetail的Dim info=DirectCast(HttpContext.Current.Session(“UploadDetail”),UploadDetail)
如果info不是空的,而且info.IsReady也是空的,那么
Dim soFar As Integer=info.UploadedLength
Dim总计为整数=info.ContentLength
整数形式完成的尺寸百分比=CInt(数学上限(CDbl(soFar)/CDbl(总计)*100))
将消息设置为字符串=“正在上载…”
Dim文件名为String=String.Format(“{0}”,info.fileName)
Dim downloadBytes As String=String.Format({1}字节中的{0}),soFar,total)
返回带有{_
Key.percentComplete=完成百分比_
Key.message=消息_
Key.fileName=文件名_
Key.downloadBytes=下载字节_
}
如果结束
一无所获
端函数
-Anonymous Typestry获得初步翻译(在本例中,翻译看起来不错)感谢@Bala这是一个很好的工具..感谢@Jonas提供的链接非常有用..这不太可能对不知道匿名初始值设定者是什么的人有所帮助。即使在VB.NET中,它们也使用了一些看起来很奇怪的大括号语法…:-)实际上,如果在创建对象的同一程序集中读取该对象,则可以使用“示例强制转换”技巧来读取匿名类型的实例,并避免反射或动态。但是,如果在同一个程序集中执行此操作,那么为什么不创建一个内部标称类型呢?