2
我有一个数据结构与几个嵌套类型。如何正确挂钩到Json.NET的JObject.ToObject <T>()?
public class MyDataType {
public int InferrableInt { get; set; }
public float InferrableFloat { get; set; }
public InferrableNestedDataType MyINDT { get; set; }
public CustomNestedDataType MyCNDT { get; set; }
public class InferrableNestedDataType {
public int X { get; set; }
public int Y { get; set; }
}
public class CustomNestedDataType {
// Data processed in a way that can't be expressed through JSON
public string CustomData { get; set; }
public int NestedInferrableInt { get; set; }
}
}
大多数这些嵌套类型可以由默认行为推断。
我还有一个JObject
从另一部分代码中收到。我想能够:
var mydt = jobjectInstance.ToObject<MyDataType>()
...只有反序列化MyDataType.CustomNestedDataType
时运行自定义代码。我找到的每个解决方案都扩展了JsonConverter
,但据我所知,这只适用于从原始JSON解析而不是预解析的JObject
。没有为可推断的数据类型编写自定义代码,这样做的正确方法是什么?
啊哈,有道理。谢谢! –