inheritance字典的Serialize类不是序列化属性

我有一个inheritance自Dictionary的类,并且有一些属性。 当我序列化它时,只序列化字典而不是属性。 如果我有一个包含属性的有效负载,它会对它们进行反序列化。 如何使其序列化我的对象,包括属性?

public class Maintenance : Dictionary { public int PersonId { get; set; } } return JsonConvert.DeserializeObject(jsonString); //Populates PersonId and all other properties end up in the dictionary. return JsonConvert.SerializeObject(maintenance); //Only returns dictionary contents 

我很欣赏有关如何使序列化使用与反序列化似乎使用相同的行为的建议。

来自Json.Net文档:“请注意,序列化时只会将字典名称/值写入JSON对象,并且在反序列化时,JSON对象上的属性将添加到字典的名称/值中..NET字典上的其他成员在序列化期间被忽略。“

取自以下链接: http : //www.newtonsoft.com/json/help/html/SerializationGuide.htm

假设您希望字典的键和值与json输出中的PersonId在同一层次结构级别上,您可以使用带有组合而不是inheritance的JsonExtensionData属性,如下所示:

 public class Maintenance { public int PersonId { get; set; } [JsonExtensionData] public Dictionary ThisNameWillNotBeInTheJson { get; set; } } 

另请看这个问题: 如何使用Json.Net将Dictionary序列化为其父对象的一部分