数据合同和数据成员

数据合同和数据成员

本文介绍了数据合同和数据成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以告诉 WCF 在返回时序列化整个类?我真的必须将 DataMember 添加到每个属性中吗?

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?

推荐答案

自 .NET 3.5 SP1 起,您无需再执行此操作.

Since .NET 3.5 SP1, you don't have to do this anymore.

如果您没有任何 [DataContract][DataMember] 属性,DataContractSerializer 类的行为将与旧的 XmlSerializer 类似:它将序列化所有公共读取/编写类中列出的属性.

If you don't have any [DataContract] and [DataMember] attributes, the DataContractSerializer class will behave like the old XmlSerializer: it will serialize all public read/write properties that are listed in the class.

虽然在这个过程中你确实失去了一些东西:

You do lose a few things in the process though:

  • 由于您没有 [DataMember] 属性,您不能再定义字段的顺序 - 它们将按照出现的顺序进行序列化

  • since you don't have [DataMember] attributes, you cannot define an order of the fields anymore - they'll be serialized in the order of appearance

您不能省略公共属性(因为这需要在所有其他属性/字段上使用 [DataMember])

you cannot omit a public property (since that would require [DataMember] on all other properties/fields)

您不能将属性定义为 Required(这将再次出现在 [DataMember] 属性上)

you cannot define a property to be Required (that would be on the [DataMember] attribute again)

你的类现在需要有一个公共的、无参数的构造函数(数据契约通常不需要)

your class now needs to have a public, parameter-less constructor (usually not needed for data contracts)

详细阅读所有相关内容 来自 Pluralsight 的 Aaron Skonnard.

Read all about it in detail from Aaron Skonnard at Pluralsight.

这篇关于数据合同和数据成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 11:46