gpt4 book ai didi

WCF - 是否在 .NET 3.5 SP1 中使用 [DataContract]?

转载 作者:行者123 更新时间:2023-12-04 13:44:28 25 4
gpt4 key购买 nike

我正在使用 WCF .NET 3.5 SP1,并且读到不必使用 [DataMember]、[DataConract] 和/或 [Serializable] 等东西来装饰它们的实体/集合?最好的方法是什么?你们都遇到了什么?

我使用的是 3.5 SP1。

最佳答案

参见 Using Data Contracts .

New complex types that you create must have a data contract defined for them to be serializable. By default, the DataContractSerializer infers the data contract and serializes all publicly visible types. All public read/write properties and fields of the type are serialized. You can opt out members from serialization by using the IgnoreDataMemberAttribute. You can also explicitly create a data contract by using DataContractAttribute and DataMemberAttribute attributes. This is normally done by applying the DataContractAttribute attribute to the type. This attribute can be applied to classes, structures, and enumerations. 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. For more information, see Serializable Types.

就像@Terry 所说的那样,主动声明您想要公开的属性可能会更好。通过这种方式,您可以在未来证明您的代码不会在基类将来添加公共(public)属性时无意中暴露字段。

关于WCF - 是否在 .NET 3.5 SP1 中使用 [DataContract]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1076480/

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