【问题标题】:Is there any way to pass an undefined object as parameter? c#有没有办法将未定义的对象作为参数传递? C#
【发布时间】:2017-09-25 07:22:25
【问题描述】:

我有一个方法适用于已定义的数据对象类型,如下所示:

public static ItemEdificio JSONtoOBJECT(this string JSONstring)
{
   return new JavaScriptSerializer().Deserialize<ItemEdificio>(JSONstring);
}

有没有什么办法可以将“ItemEdificio”转换为实际根据对象类型而有所不同的东西?

请记住,这是使用 Newtonsoft 库制作的。

【问题讨论】:

  • 这就是泛型的用途,例如:MyType a = myJsonString.JSONtoOBJECT&lt;MyType&gt;()

标签: c# object dynamic json.net


【解决方案1】:
public static T JSONtoOBJECT<T>(this string JSONstring)
{
    return new JavaScriptSerializer().Deserialize<T>(JSONstring);
}

这就是泛型 (https://msdn.microsoft.com/en-us/library/0x6a29h6.aspx) 的用途。

你这样称呼它:MyType instance = jsonData.JSONtoOBJECT&lt;MyType&gt;();

【讨论】:

    猜你喜欢
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 2017-07-10
    • 2015-07-08
    • 2021-12-20
    • 2020-11-30
    • 1970-01-01
    相关资源
    最近更新 更多