问题描述
我对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; }
}
它工作正常,但是当我删除 DataContract时
和 DataMember
也可以正常工作。我不明白为什么它可以正常工作。谁能告诉我 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);
}
推荐答案
Microsoft .NET 3.5 SP1的 [DataContract]
和 [DataMember]
属性不堪重负,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] $ c $的情况下序列化非公共属性或字段
- c>,您无法定义序列化顺序(
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 likeIsRequired=
or other useful attributes - without
[DataMember]
, you cannot leave out certain public properties - all public properties will be serialized by the DCS
因此,对于快速'肮脏解决方案,省去 [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属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!