WCF Study Notes-DataContract equivalence

Source: Internet
Author: User

The reason why the client and server of WCF can smoothly transmit a certain type of data is not that the receiving type of the receiver is the same as that of the sender, dataContract, which is represented by two types, is equivalent.

The so-called equivalence means that the namespace and name of DataContract, and the name and category of its data fields are consistent.

Generally, the equivalence of DataContract is defined by the Name attribute of DataContractAttriubte and DataMemberAttribute. By default, the value of the Name attribute is the Name and attribute Name of the class (struct. Therefore, the following definitions have the same effect:

[C #] [DataContract]
Class Contact {...}
 
[DataContract (Name = "Contact")]
Class Contact {...}

[DataContract]
Class Contact {...}

[DataContract (Name = "Contact")]
Class Contact {...} has the same effect as the following fields:

[C #] view plaincopyprint? [DataMember]
Public string Name;
 
[DataMember (Name = "Name")]
Public string Name;

[DataMember]
Public string Name;

[DataMember (Name = "Name")]
Public string Name; therefore, you can use the Name fields of DataContractAttribute and DataMemberAttribute to implement different data types that can represent equivalent DataContract, for example:

[C #] [DataContract]
Class Contact
{
[DataMember]
Public string FirstName;

[DataMember]
Public string LastName;
}
 
[DataContract (Name = "Contact")]
Class Person
{
[DataMember (Name = "FirstName")]
Public string Name;

[DataMember (Name = "LastName")]
Public string Surname;
}

[DataContract]
Class Contact
{
[DataMember]
Public string FirstName;

[DataMember]
Public string LastName;
}

[DataContract (Name = "Contact")]
Class Person
{
[DataMember (Name = "FirstName")]
Public string Name;

[DataMember (Name = "LastName")]
Public string Surname;
} But there is another attribute of DataMemberAttribute, Which is Order. The two classes defined below are not equivalent.

[C #] [DataContract]
Class Contact
{
[DataMember]
Public string FirstName;

[DataMember]
Public string LastName;
}
 
[DataContract (Name = "Contact")]
Class Person
{
[DataMember (Name = "FirstName", Order = 2)]
Public string Name;

[DataMember (Name = "LastName", Order = 1)]
Public string Surname;
}

[DataContract]
Class Contact
{
[DataMember]
Public string FirstName;

[DataMember]
Public string LastName;
}

[DataContract (Name = "Contact")]
Class Person
{
[DataMember (Name = "FirstName", Order = 2)]
Public string Name;

[DataMember (Name = "LastName", Order = 1)]
Public string Surname;
} The Order attribute determines the Order of DataContract serialization and deserialization. By default, both serialization and deserialization are from top to bottom. If there is an inheritance relationship, it is also based on the sequence of the first base class and then the subclass. However, if you use the Name attribute of DataMember to rename the DataContract field, the default serialization order may be disrupted. The following definition is not equivalent either:

[C #] [DataContract]
Class Contact
{
[DataMember]
Public string FirstName;

[DataMember]
Public string LastName;

[DataMember]
Public int Age;
}
 
[DataContract (Name = "Contact")]
Class Person
{
[DataMember (Name = "FirstName")]
Public string Name;

[DataMember (Name = "LastName")]
Public string Surname;

[DataMember]
Public int Age;
}

[DataContract]
Class Contact
{
[DataMember]
Public string FirstName;

[DataMember]
Public string LastName;

[DataMember]
Public int Age;
}

[DataContract (Name = "Contact")]
Class Person
{
[DataMember (Name = "FirstName")]
Public string Name;

[DataMember (Name = "LastName")]
Public string Surname;

[DataMember]
Public int Age;
} The serialization sequence of the Person class is actually Age, FirstName, and LastName. Therefore, you need to add the Order attribute to the display. If the Order attribute in the class is equal, then it will be serialized and deserialized in the top-down order.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.