【发布时间】:2018-09-16 05:29:13
【问题描述】:
我有扩展 DynamicObject 类的类 foo。 该类还包含一个 Dictionary 类型的属性。
当我尝试使用 Newton.Soft Json 转换器对其进行序列化时。我将“{}”作为空白对象。
以下是我的代码:
public class Foo: DynamicObject
{
/// <summary>
/// Gets or sets the properties.
/// </summary>
/// <value>The properties.</value>
public Dictionary<string, object> Properties { get; set; } = new Dictionary<string, object>();
/// <summary>
/// Gets the count.
/// </summary>
/// <value>The count.</value>
public int Count => Properties.Keys.Count;
}
现在我提到了,在对其进行序列化时,我得到了空白对象。 下面是序列化的代码:
public static void Main()
{
Foo foo= new Foo();
foo.Properties = new Dictionary<string, object>()
{
{"SomeId", 123},
{"DataType","UnKnonw"},
{"SomeOtherId", 456},
{"EmpName", "Pranay Deep"},
{"EmpId", "789"},
{"RandomProperty", "576Wow_Omg"}
};
//Now serializing..
string jsonFoo = JsonConvert.SerializeObject(foo);
//Here jsonFoo = "{}".. why?
Foo foo2= JsonConvert.DeserializeObject<Foo>(jsonFoo);
}
如果我遗漏了什么,请告诉我?
【问题讨论】:
标签: c# .net json serialization json.net