这里有什么问题吗?我很困惑...ToJSON 扩展方法

发布于 2024-09-15 04:58:08 字数 1662 浏览 5 评论 0原文

给出以下声明:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

银河中√捞星星 2024-09-22 04:58:08

第三个 Put 的问题是 VB 不支持任何声明为 Object 类型的扩展方法:VB.NET:无法在 System.Object 实例上使用扩展方法

这将起作用: Put(ToJSON(kvp.Value))

这也是如此:

Dim kvp = New System.Collections.Generic.KeyValuePair(Of String, HttpCookie)( 
    "date", New HttpCookie("woot", "yikes")) 
Put(kvp.Value.ToJSON) 

The problem with the third Put is that VB doesn't support extension methods on anything declared to be of type Object: VB.NET: impossible to use Extension method on System.Object instance

This will work: Put(ToJSON(kvp.Value))

And so will this:

Dim kvp = New System.Collections.Generic.KeyValuePair(Of String, HttpCookie)( 
    "date", New HttpCookie("woot", "yikes")) 
Put(kvp.Value.ToJSON) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文