WCF DataContract 需要序列化吗?
具有[DataContract]属性的类,不是会自动序列化吗? 如果不是,是否需要使用 [Serializable] 属性(或从 ISerialize 继承)?
我问这个问题是因为我有以前项目中的旧代码,显然 [DataContract] 类没有明确提及任何有关序列化的内容。
最终问题: 我想要一个 WCF 操作(方法)来返回 DataContract 类。 该方法是否必须在返回类之前显式序列化该类,还是自动序列化?我一直以为会是后者。
谢谢!
A class that has the [DataContract] attribute, is it not automatically serialized?
If not, is it a requirement to use the [Serializable] attribute (or inherit from ISerialize)?
I ask this because I have old code from a previous project that apparently the [DataContract] classes do not mention anything, explicitly, about serialization.
The ultimate question:
I want a WCF operation (method) to return a DataContract class.
Does the method have to explicitly serialize the class before returning it, or is it automatically serialized? I always thought it would be the latter.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,
[Serialized]
在数据合约中不是必需的。 XML 序列化也不是必需的。No,
[Serializable]
is not necessary on a data contract. It is also not necessary for XML Serialization..NET 中有一些序列化选项。 SerializedAttribute 和 ISerialized 回到最初,被 BinaryFormatter、SoapFormatter 等使用。DataContractSerializer
相对较新,可以支持使用 SerializedAttribute 定义其序列化能力的对象,但这不是必需的。如果您只是使用 DataContractSerializer 序列化对象,那么您只需使用数据协定属性即可。
显然,标记为可序列化的成员也必须是可序列化类型。
有关详细信息,请参阅 MSDN 上的数据协定序列化程序支持的类型。
There's a few options for serialization in .NET. SerializableAttribute and ISerializable go back to the beginning and are used by the BinaryFormatter, SoapFormatter, etc.
DataContractSerializer, being relatively new, can support objects that define their serialization ability with SerializableAttribute, but it's not necessary. If you are just serializing the object using DataContractSerializer then using the data contract attributes is all you need to do.
Obviously the members you mark as serializable must also be of serializable types.
For more information, see Types Supported by the Data Contract Serializer on MSDN.
除此之外,DataContractSerializer 支持的不仅仅是 DataContract 类型。请参阅这篇优秀的博客文章,详细了解 DataContractSerializer 支持的整个类型:http://blogs.msdn.com/b/sowmy/archive/2006/02/22/536747.aspx
Just to add on to this, DataContractSerializer supports far more than just DataContract types. See this excellent blog post for a detailed walk-through of the entire universe of types supported by DataContractSerializer: http://blogs.msdn.com/b/sowmy/archive/2006/02/22/536747.aspx