开发者

WCF DataContract requires Serialization?

开发者 https://www.devze.com 2023-01-31 11:52 出处:网络
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)?

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 serial开发者_开发问答ize the class before returning it, or is it automatically serialized? I always thought it would be the latter.

Thanks!


No, [Serializable] is not necessary on a data contract. It is also not necessary for XML Serialization.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消