<legend id='12VmT'><style id='12VmT'><dir id='12VmT'><q id='12VmT'></q></dir></style></legend>

      <i id='12VmT'><tr id='12VmT'><dt id='12VmT'><q id='12VmT'><span id='12VmT'><b id='12VmT'><form id='12VmT'><ins id='12VmT'></ins><ul id='12VmT'></ul><sub id='12VmT'></sub></form><legend id='12VmT'></legend><bdo id='12VmT'><pre id='12VmT'><center id='12VmT'></center></pre></bdo></b><th id='12VmT'></th></span></q></dt></tr></i><div id='12VmT'><tfoot id='12VmT'></tfoot><dl id='12VmT'><fieldset id='12VmT'></fieldset></dl></div>

        <tfoot id='12VmT'></tfoot>
          <bdo id='12VmT'></bdo><ul id='12VmT'></ul>

        <small id='12VmT'></small><noframes id='12VmT'>

        使用 System.Type 作为 &lt;T&gt;在运行时使用

        时间:2023-08-23

          <legend id='1AVif'><style id='1AVif'><dir id='1AVif'><q id='1AVif'></q></dir></style></legend>
            <bdo id='1AVif'></bdo><ul id='1AVif'></ul>
            <i id='1AVif'><tr id='1AVif'><dt id='1AVif'><q id='1AVif'><span id='1AVif'><b id='1AVif'><form id='1AVif'><ins id='1AVif'></ins><ul id='1AVif'></ul><sub id='1AVif'></sub></form><legend id='1AVif'></legend><bdo id='1AVif'><pre id='1AVif'><center id='1AVif'></center></pre></bdo></b><th id='1AVif'></th></span></q></dt></tr></i><div id='1AVif'><tfoot id='1AVif'></tfoot><dl id='1AVif'><fieldset id='1AVif'></fieldset></dl></div>
              • <small id='1AVif'></small><noframes id='1AVif'>

                  <tfoot id='1AVif'></tfoot>
                    <tbody id='1AVif'></tbody>

                1. 本文介绍了使用 System.Type 作为 &lt;T&gt;在运行时使用 Json.Net 反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个进程需要能够以下列方式调用函数.

                  I have a process that needs to be able to call on a function in the following manner.

                  public static Task<string> Convert(string payload, Type type)
                  {
                      JsonSerializerSettings settings = new JsonSerializerSettings().Configure();//<--- Pull in extension methods to handle custom types
                      return JsonConvert.SerializeObject(JsonConvert.DeserializeObject<type>(payload), settings));
                  }
                  

                  上面的代码无法编译.接下来,我使用 John Skeet 的回答 来回答 2011 年类似的问题去我的.

                  The code above does not compile. Next, I used John Skeet's answer to a 2011 question similar to mine.

                  public static string Convert(string payload, Type type)
                  {
                      JsonSerializerSettings settings = new JsonSerializerSettings().Configure();
                      //This clashes with another overload of DeserializeObject in the method table
                      MethodInfo method = typeof(JsonConvert).GetMethod("DeserializeObject", new[] { typeof(string) });
                      method = method.MakeGenericMethod(type);
                      object[] arguments = new object[1];
                      arguments[0] = payload;
                      string result = (string)method.Invoke(null, arguments);
                  
                      return JsonConvert.SerializeObject(result, settings);
                  }
                  

                  存在同名的非泛型重载和泛型重载,并且方法表查找失败.我可以通过调用类似于下面的函数(非模棱两可)来解决这个问题.

                  There is an overload non-generic and a generic overload with the same name and the method table lookup fails. I was able to get around that problem by calling into my function (non-ambiguous) similar to below.

                  public static string Convert(string payload, Type type)
                  {
                      JsonSerializerSettings settings = new JsonSerializerSettings().Configure();
                      //This clashes with another overload of DeserializeObject in the method table
                      MethodInfo method = typeof(TestConverter).GetMethod("TestThis", new[] { typeof(string) });
                      method = method.MakeGenericMethod(type);
                      object[] arguments = new object[1];
                      arguments[0] = payload;
                      string result = (string)method.Invoke(null, arguments);
                  
                      return JsonConvert.SerializeObject(result, settings);
                  }
                  
                  public static T TestThis<T>(string s)
                  {
                      return JsonConvert.DeserializeObject<T>(s);
                  }
                  

                  但是,我现在回到了我的自定义转换扩展方法被忽略的地方,因为反序列化程序忽略了我的 Type 类型.

                  However, I am now back to where my custom conversion extension methods are being ignored because the de-serializer is ignoring my Type type.

                  这是否可能.当然我可以使用类似的反序列化版本:

                  Is this even possible. Of courseI could use the de-serialization version similar to:

                  object result = JsonConvert.DeserializeObject(payload);
                  

                  但是,我的练习是使用 JsonConvert.DeserializeObject<T>(payload) 重载,以便可以调用我的转换器.

                  However, my exercise is to use the JsonConvert.DeserializeObject<T>(payload) overload so that my converters can be invoked.

                  有什么建议吗?

                  推荐答案

                  你不需要跳槽.JsonConvert.DeserializeObject 有一个非接受 Type 的通用 重载.

                  You do not need to jump through hoops. JsonConvert.DeserializeObject has a non-generic overload that accepts a Type.

                  public static Task<string> Convert(string payload, Type type)
                  {
                      JsonSerializerSettings settings = new JsonSerializerSettings().Configure();//<--- Pull in extension methods to handle custom types
                      return JsonConvert.SerializeObject(JsonConvert.DeserializeObject(payload, type), settings));
                  }
                  

                  这篇关于使用 System.Type 作为 &lt;T&gt;在运行时使用 Json.Net 反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 Json.Net 在 json 对象后丢弃垃圾字符 下一篇:当值可以是对象或空数组时反序列化 JSON

                  相关文章

                  最新文章

                    <legend id='JEnEE'><style id='JEnEE'><dir id='JEnEE'><q id='JEnEE'></q></dir></style></legend>

                  1. <small id='JEnEE'></small><noframes id='JEnEE'>

                    <tfoot id='JEnEE'></tfoot>
                    • <bdo id='JEnEE'></bdo><ul id='JEnEE'></ul>
                  2. <i id='JEnEE'><tr id='JEnEE'><dt id='JEnEE'><q id='JEnEE'><span id='JEnEE'><b id='JEnEE'><form id='JEnEE'><ins id='JEnEE'></ins><ul id='JEnEE'></ul><sub id='JEnEE'></sub></form><legend id='JEnEE'></legend><bdo id='JEnEE'><pre id='JEnEE'><center id='JEnEE'></center></pre></bdo></b><th id='JEnEE'></th></span></q></dt></tr></i><div id='JEnEE'><tfoot id='JEnEE'></tfoot><dl id='JEnEE'><fieldset id='JEnEE'></fieldset></dl></div>