使用 System.Type 作为 <T>在运行时使用 Json.Net 反序列化

Using System.Type as lt;Tgt; at runtime when deserializing with Json.Net(使用 System.Type 作为 lt;Tgt;在运行时使用 Json.Net 反序列化)

本文介绍了使用 System.Type 作为 <T>在运行时使用 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 反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 System.Type 作为 &lt;T&gt;在运行时使用 Json.Net 反序列化

基础教程推荐