对于一个带有[DataContract]属性标记的类,其需要序列化的私有(private)字段是否标记为[DataMember]?
示例:
[DataContract]
public class Component
{
// [DataMember] is not needed since public fields get automatically serialized
public int number;
// do I need [DataMember] here?
private string characters;
// [DataMember] is required here, but I also need to include the
// attribute [DataMember] in this class's definition
private complexType cT;
我正在阅读DataContractAttribute Class正确,对吧?
最佳答案
不,您似乎没有正确阅读文档。
数据契约是一种公开共享信息的方式,与常规序列化略有不同。
从您链接的文档页面:
The DataMemberAttribute attribute must then be applied to each member of the data contract type to indicate that it is a data member, that is, it should be serialized.
但这仅适用于您像以前一样添加 [DataContract]
属性的情况。
关于私有(private)字段的 C# DataContract 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8883615/