且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

json.net 不序列化派生类的属性

更新时间:2023-01-16 10:54:56

是的,您缺少派生类的 [DataContract] 属性.您还需要将 [DataMember] 添加到您想要序列化的任何属性或字段(如果您尚未添加它们).Json.Net 在 5.0 版本 1 中进行了更改(4 月2013)这样 [DataContract] 属性不会被继承.

Yes, you are missing the [DataContract] attribute on the derived class. You also need to add [DataMember] to any properties or fields that you want serialized, if you haven't already added them. Json.Net was changed in version 5.0 release 1 (April 2013) such that the [DataContract] attribute is not inherited.

请注意,如果您从您的类中删除所有 [DataContract][DataMemeber] 实例,Json.Net 的行为会有所不同:在在这种情况下,Json.Net 的默认行为是序列化所有公共属性,包括基类和派生类.

Note that if you remove all instances of [DataContract] and [DataMemeber] from your classes, Json.Net behaves differently: in that case, the default behavior is for Json.Net to serialize all public properties, both in the base and derived classes.