WCF学习笔记-枚举类型
枚举类型在WCF中默认是可以被序列化的,而不需要显式的使用DataContractAttribute来修饰。因此如下代码是可以正常运行的:
[c#]
enum ContactType
{
Customer,
Vendor,
Partner
}
[DataContract]
class Contact
{
[DataMember]
public ContactType ContactType;
....
}
enum ContactType
{
Customer,
Vendor,
Partner
}
[DataContract]
class Contact
{
[DataMember]
public ContactType ContactType;
....
}如果要在禁止某一个枚举值,就需要显示的使用DataContractAttribute来修饰了,并且需要EnumMemberAttribute来修饰枚举值,没有被EnumMemberAttriubute修饰的枚举值就表示不被包含在数据契约中。如要因此枚举ContactType的Partner枚举值的话,可以使用如下代码:
[c#]
[DataContract]
enum ContactType
{
[EnumMember]
Customer,
[EnumMember]
Vendor,
// will not be part of data contract.
Partner
}
[DataContract]
enum ContactType
{
[EnumMember]
Customer,
[EnumMember]
Vendor,
// will not be part of data contract.
Partner
}等同于:
[c#]
enum ContactType
{
Customer,
Vendor
}
enum ContactType
{
Customer,
Vendor
}另外EnumMemberAttribute有一个Value属性,可以改变枚举名在数据契约中的名称,类似于DataContractAttribute, DataMemberAttribute和OperationContractAttribute的Name属性。使用方法如下代码:
[c#]
[DataContract]
enum ContactType
{
[EnumMember(Value = "MyCustomer")]
Customer,
[EnumMember]
Vendor,
[EnumMember]
Partner
}
[DataContract]
enum ContactType
{
[EnumMember(Value = "MyCustomer")]
Customer,
[EnumMember]
Vendor,
[EnumMember]
Partner
}那么数据契约就等同于:
[c#]
enum ContactType
{
MyCustomer,
Vendor,
Partner
}
enum ContactType
{
MyCustomer,
Vendor,
Partner
推荐阅读