Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF: DataMember attribute on property vs. member

In wcf, what is the difference between applying the DataMember attribute on a property

private int m_SomeValue;  [DataMember]   public int SomeValue {   get {...}   set {...} } 

instead of a member variable

[DataMember]   private int m_SomeValue;  public int SomeValue {   get {...}   set {...} } 

?

like image 882
Julien Poulin Avatar asked Feb 17 '09 13:02

Julien Poulin


People also ask

Is DataMember attribute required?

You must specify [DataMember] attribute on the property or the field of your Data Contract class to identify it as a Data Member. DataContractSerializer will serialize only those members, which are annotated by [DataMemeber] attribute.

What is Datacontract and DataMember in WCF?

A datacontract is a formal agreement between a client and service that abstractly describes the data to be exchanged. In WCF, the most common way of serialization is to make the type with the datacontract attribute and each member as datamember.

Which attribute specifies that a public property of a class is capable of being serialized?

Applying the DataMemberAttribute to a field or property explicitly specifies that the member value will be serialized. In contrast, the BinaryFormatter serializes public and private fields of a type, and the XmlSerializer serializes only public fields and properties of a type.

What is Datacontract attribute?

[DataContract] attribute specifies the data, which is to serialize (in short conversion of structured data into some format like Binary, XML etc.) and deserialize(opposite of serialization) in order to exchange between the client and the Service.


1 Answers

In general, you should favor applying the DataMember attribute on the property, rather than on the private field. The only reason to apply the attribute to the field instead is if the property were read-only (i.e. it has no setter).

like image 158
dthrasher Avatar answered Oct 11 '22 00:10

dthrasher