WCF Study Notes-Enumeration type

Source: Internet
Author: User

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
 

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.