Enumeration types can be serialized by default in WCF, without explicitly modifying them using DataContractAttribute. Therefore, the following code runs properly:
[C #]
Enum ContactType
{
Customer,
Vendor,
Partner
}
[DataContract]
Class Contact
{
[DataMember]
Public ContactType;
....
}
Enum ContactType
{
Customer,
Vendor,
Partner
}
[DataContract]
Class Contact
{
[DataMember]
Public ContactType;
....
} If you want to disable an enumerated value, you need to use DataContractAttribute to display it and EnumMemberAttribute to modify the enumerated value, the enumerated values not modified by EnumMemberAttriubute indicate that they are not included in the data contract. To enumerate the Partner enumeration values of ContactType, use the following code:
[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
} Is equivalent:
[C #]
Enum ContactType
{
Customer,
Vendor
}
Enum ContactType
{
Customer,
Vendor
} In addition, EnumMemberAttribute has a Value attribute that can change the Name of an enumeration Name in a data contract, similar to the Name attribute of DataContractAttribute, DataMemberAttribute, and OperationContractAttribute. Use the following code:
[C #]
[DataContract]
Enum ContactType
{
[EnumMember (Value = "MyCustomer")]
Customer,
[EnumMember]
Vendor,
[EnumMember]
Partner
}
[DataContract]
Enum ContactType
{
[EnumMember (Value = "MyCustomer")]
Customer,
[EnumMember]
Vendor,
[EnumMember]
Partner
} The data contract is equivalent:
[C #]
Enum ContactType
{
MyCustomer,
Vendor,
Partner
}
Enum ContactType
{
MyCustomer,
Vendor,
Partner