WCF学习笔记-DataContract等价性
WCF的客户端和服务端之所以能顺利的传输某一类型的数据,其实不在于接收方的接收类型和发送方的发送类型一致,而实际是两个类型所代表的DataContract是等价的。
所谓等价性是说DataContract的命名空间和名称,及其数据字段的名称,类别也是一致的。
通常DataContract的等价性是通过DataContractAttriubte和DataMemberAttribute的Name属性来定义的,默认情况下Name属性的值就是类(结构体)的名字和属性的名字。因此如下定义效果是一样的:
[c#] [DataContract]
class Contact { ... }
[DataContract(Name = "Contact")]
class Contact { ... }
[DataContract]
class Contact { ... }
[DataContract(Name = "Contact")]
class Contact { ... }同样的,如下字段的定义效果也是一样的:
[c#] view plaincopyprint?[DataMember]
public string Name;
[DataMember(Name = "Name")]
public string Name;
[DataMember]
public string Name;
[DataMember(Name = "Name")]
public string Name;因此,通过DataContractAttribute和DataMemberAttribute的Name字段可以实现不同的数据类型可以表示等价的DataContract,如:
[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;
}但是DataMemberAttribute还有一个属性是Order,如下定义的两个类则不是等价的。
[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;
}Order属性决定了DataContract序列化和反序列化的顺序,默认情况下,序列化和反序列化都是自上往下的,如果有继承关系,也是按先基类后子类的顺序。但是有一点需要注意就是,如果使用DataMember的Name属性重新命名DataContract的字段时,可能会打乱默认的序列化顺序。如下定义也不是等价的:
[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;
}Person类的序列化顺序其实是Age, FirstName, LastName,因此需要显示的添加Order属性,如果类中的Order属性是相等的,那么就会按照自顶向下的顺序序列化和反序列化。
补充:软件开发 , C# ,