本文介绍了何时使用 DataContract 和 DataMember 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 WCF 中的 DataContract 属性感到非常困惑.据我所知,它用于序列化用户定义的类型,如类.我写了一个这样在客户端暴露的类.

I am very confused about the DataContract attribute in WCF. As per my knowledge it is used for serializating user defined type like classes. I wrote one class which is exposed at client side like this.

[DataContract]
public class Contact
{
    [DataMember]
    public int Roll { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Address { get; set; }

    [DataMember]
    public int Age { get; set; }
}

它工作正常,但是当我删除 DataContractDataMember 时,它也能正常工作.我不明白为什么它可以正常工作.谁能告诉我DataContract的实际用途是什么?

It is working properly but when I remove DataContract and DataMember it also works properly. I can't understand why it is working properly. Can any one tell me what is the actual use of DataContract?

我的服务合同是这样的

[ServiceContract]
public interface IRestServiceImpl
{
    [OperationContract]
    Contact XmlData(string id);
}

推荐答案

由于很多程序员对 [DataContract][DataMember] 属性感到不知所措,在 .NET 3.5 SP1 中,Microsoft 使数据协定序列化程序处理所有类 - 即使没有任何这些属性 - 很像旧的 XML 序列化程序.

Since a lot of programmers were overwhelmed with the [DataContract] and [DataMember] attributes, with .NET 3.5 SP1, Microsoft made the data contract serializer handle all classes - even without any of those attributes - much like the old XML serializer.

因此,从 .NET 3.5 SP1 开始,您不再必须添加数据协定或数据成员属性 - 如果您不这样做,那么数据协定序列化程序将序列化您的所有公共属性类,就像 XML 序列化程序一样.

So as of .NET 3.5 SP1, you don't have to add data contract or data member attributes anymore - if you don't then the data contract serializer will serialize all public properties on your class, just like the XML serializer would.

然而:如果不添加这些属性,您将失去很多有用的功能:

HOWEVER: by not adding those attributes, you lose a lot of useful capabilities:

  • 如果没有 [DataContract],您将无法为数据定义 XML 命名空间
  • 如果没有 [DataMember],您将无法序列化非公共属性或字段
  • 如果没有 [DataMember],您将无法定义序列化顺序 (Order=),DCS 将按字母顺序序列化所有属性
  • 如果没有 [DataMember],您将无法为您的属性定义不同的名称 (Name=)
  • 如果没有 [DataMember],您将无法定义诸如 IsRequired= 之类的东西或其他有用的属性
  • 如果没有[DataMember],您就不能遗漏某些公共属性——所有公共属性都将由 DCS 序列化
  • without [DataContract], you cannot define an XML namespace for your data to live in
  • without [DataMember], you cannot serialize non-public properties or fields
  • without [DataMember], you cannot define an order of serialization (Order=) and the DCS will serialize all properties alphabetically
  • without [DataMember], you cannot define a different name for your property (Name=)
  • without [DataMember], you cannot define things like IsRequired= or other useful attributes
  • without [DataMember], you cannot leave out certain public properties - all public properties will be serialized by the DCS

所以对于quick'n'dirty"的解决方案,去掉 [DataContract][DataMember] 属性是可行的 - 但它仍然是一个好主意将它们放在您的数据类中 - 只是为了更明确地说明您在做什么,并让您自己访问所有那些没有它们就无法获得的附加功能......

So for a "quick'n'dirty" solution, leaving away the [DataContract] and [DataMember] attributes will work - but it's still a good idea to have them on your data classes - just to be more explicit about what you're doing, and to give yourself access to all those additional features that you don't get without them...

这篇关于何时使用 DataContract 和 DataMember 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-11 01:41