DataContracts and DataMembers

后端 未结 2 1632
小蘑菇
小蘑菇 2021-02-13 11:59

Are there any ways to tell WCF to serialize the whole class when it returns? Do I literally have to add DataMember to every property?

2条回答
  •  情深已故
    2021-02-13 12:10

    I love marc's answer, but I want to add some more info.

    DataContractSerializer and DataContractJsonSerializer both support, out of the box, many other serialization models as well. This includes IXmlSerializable, Serializable, and ISerializable. POCO support was added in .NET 3.5 SP1, but support for these other models has always been there since .NET 3

    This blog post details the extent of the support and more importantly, the prioritization of different models by the serializer (i.e., it tells you what DataContract-based serializers would do if you have one type decorated with multiple serialization models)

    So, if you read that blog post, you'll notice that POCO support is last in the priority list. It is the serializer's last resort, if there is absolutely no other serialization programming model available on the type or its parent. For example, if the type is an enumerable of some sort, it will get serializaed according to traditional collection rules. If it's ISerializable or Serializable, it will get serialized according to their serialization rules.

    Another important difference: during deserialization of all other types, the default zero-params constructor is never called. For POCO types, it is always called! This gives you an additional hook you don't have so easily in other serialization models!

提交回复
热议问题