gpt4 book ai didi

私有(private)字段的 C# DataContract 属性?

转载 作者:行者123 更新时间:2023-11-30 22:32:05 25 4
gpt4 key购买 nike

对于一个标有属性[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正确,对吧?

最佳答案

不,看起来您没有正确阅读文档。

DataContracts 是一种公开共享信息的方式,与常规序列化略有不同。

从您链接的文档页面:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com