Using System.Type as lt;Tgt; at runtime when deserializing with Json.Net(使用 System.Type 作为 lt;Tgt;在运行时使用 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 作为 <T>在运行时使用 Json.Net 反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 System.Type 作为 <T>在运行时使用 Json.Net 反序列化
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- 使用 rss + c# 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01