这里有什么问题吗?我很困惑...ToJSON 扩展方法
给出以下声明:
<Extension()> Public Function ToJSON(ByVal target As Object) As String
Dim serializer = New System.Runtime.Serialization.Json.DataContractJsonSerializer(target.GetType)
Using ms As MemoryStream = New MemoryStream()
serializer.WriteObject(ms, target)
ms.Flush()
Dim bytes As Byte() = ms.ToArray()
Dim json As String = Encoding.UTF8.GetString(bytes, 0, bytes.Length)
Return json
End Using
End Function
以及测试页的 Page_Load 中的以下几行:
Dim kvp = New System.Collections.Generic.KeyValuePair(Of String, Object)(
"date", New HttpCookie("woot", "yikes")
)
Put(New HttpCookie("woot", "yikes").ToJSON)
Put(kvp.ToJSON)
Put(kvp.Value.ToJSON)
Put("here".ToJSON())
第一个 Put 工作完美,并输出以下 JSON:
{"Domain":null,"Expires":"\/Date(-62135578800000-0500)\/",
"HttpOnly":false,"Name":"woot","Path":"\/",
"Secure":false,"Value":"yikes"}
然而,第二个 Put 抛出一个巨大且丑陋的错误,如下所示:
输入“System.Web.HttpCookie”,数据协定名称为“HttpCookie:http: //schemas.datacontract.org/2004/07/System.Web' 不是预期的。考虑使用 DataContractResolver 或将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将它们添加到传递给 DataContractSerializer 的已知类型列表中。
第三个 Put 也会引发错误,但完全不同:
未找到类型“HttpCookie”的公共成员“ToJSON”。
第四次看跌期权表现完美。
我很困惑为什么当第一行有效并且在 HttpCookie 对象上清楚地找到扩展方法时,为什么在第二次和第三次 Put 中它不起作用,为什么在这两种情况下我都会收到不同的错误?前三个 Put 中的每一个都尝试执行相同的操作 - 调用 HttpCookie 对象上的 ToJSON 扩展方法。
欢迎所有展览!
Given the following declaration:
<Extension()> Public Function ToJSON(ByVal target As Object) As String
Dim serializer = New System.Runtime.Serialization.Json.DataContractJsonSerializer(target.GetType)
Using ms As MemoryStream = New MemoryStream()
serializer.WriteObject(ms, target)
ms.Flush()
Dim bytes As Byte() = ms.ToArray()
Dim json As String = Encoding.UTF8.GetString(bytes, 0, bytes.Length)
Return json
End Using
End Function
And the following lines in the Page_Load of a test page:
Dim kvp = New System.Collections.Generic.KeyValuePair(Of String, Object)(
"date", New HttpCookie("woot", "yikes")
)
Put(New HttpCookie("woot", "yikes").ToJSON)
Put(kvp.ToJSON)
Put(kvp.Value.ToJSON)
Put("here".ToJSON())
The first Put works perfect, and puts out the following JSON:
{"Domain":null,"Expires":"\/Date(-62135578800000-0500)\/",
"HttpOnly":false,"Name":"woot","Path":"\/",
"Secure":false,"Value":"yikes"}
The second Put, however, throws a giant, ugly error as so:
Type 'System.Web.HttpCookie' with data contract name 'HttpCookie:http://schemas.datacontract.org/2004/07/System.Web' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
The third Put throws an error also, but COMPLETELY different:
Public member 'ToJSON' on type 'HttpCookie' not found.
And the fourth Put works perfect.
I am very confused why, when the first line works, and the Extension method is clearly being found on the HttpCookie object, why then in the 2nd and 3rd Puts does it NOT work, and why do I get a different error in both cases? Each of the first three Puts is trying to do the same thing - call the ToJSON extension method on the HttpCookie object.
All exposition welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第三个
Put
的问题是 VB 不支持任何声明为Object
类型的扩展方法:VB.NET:无法在 System.Object 实例上使用扩展方法这将起作用:
Put(ToJSON(kvp.Value))
这也是如此:
The problem with the third
Put
is that VB doesn't support extension methods on anything declared to be of typeObject
: VB.NET: impossible to use Extension method on System.Object instanceThis will work:
Put(ToJSON(kvp.Value))
And so will this: