Use flag enumerations in WCF

Source: Internet
Author: User

Please refer to the msdn example:

 

[Datacontract] [flags]
Public Enum Carfeatures
{
None = 0 ,
[Enummember]
Airconditioner = 1 ,
[Enummember]
Automatictransmission = 2 ,
[Enummember]
Powerdoors = 4 ,
Alloywheels = 8 ,
Deluxepackage = airconditioner | automatictransmission | powerdoors | alloywheels,
[Enummember]
Cdplayer = 16 ,
[Enummember]
Tapeplayer = 32 ,
Musicpackage = cdplayer | tapeplayer,
[Enummember]
Everything = deluxepackage | musicpackage
}

 

Note the following:

1. Use the [flags] flag.

2. All enumerated Member values that have applied enummemberattribute must be the power of uninterrupted 2 (such as 1, 2, 4, 8, 16, 32, 64 ).

3. if you use numeric values to find an enumerative member (for example, if you use 4 to find powerdoors), the system first checks whether the member exists. If the member does not exist, the system checks whether such a composite member exists, if it still does not exist and the value is not 0, a serializationexception is thrown. If the value is 0, an empty list is returned.

4. Members not marked as [enummember] cannot be used on the WCF client. None =0.

For detailed usage, see msdn.

Http://msdn.microsoft.com/en-us/library/aa347875.aspx

 

You can use simple enumerations when you do not need to customize the enumeration's data contract name and namespace and the enumeration member values.

Notes on simple enumerations

ApplyingEnummemberattributeAttribute to simple enumerations has no effect.

It makes no difference whether or notSerializableattributeAttribute is applied to the enumeration.

The fact thatDatacontractserializerClass honorsNonserializedattributeAttribute applied to enumeration members is different from the behavior of the binaryformatter and the soapformatter. Both of those serializers ignoreNonserializedattributeAttribute.

Flag enumerations

You can applyFlagsattributeAttribute to enumerations. In that case, a list of zero or more enumeration values can be sent or stored ed simultaneously.

To do so, applyDatacontractattributeAttribute to the flag enumeration and then mark all the members that are powers of two withEnummemberattributeAttribute. Note that to use a flag enumeration, the progression must be an uninterrupted sequence of powers of 2 (for example, 1, 2, 4, 8, 16, 32, 64 ).

The following steps apply to sending a flag's enumeration value:

  1. Attempt to find an enumeration member (withEnummemberattributeAttribute applied) that maps to the numeric value. If found, send a list that contains just that member.
  2. Attempt to break the numeric value into a sum such that there are enumeration members (each withEnummemberattributeAttribute applied) that map to each part of the sum. Send the list of all these members. Note thatGreedy AlgorithmIs used to find such a sum, and thus there is no guarantee that such a sum is found even if it is present. to avoid this problem, make sure that the numeric values of the enumeration members are powers of two.
  3. If the preceding two steps fail, and the numeric value is nonzero, throwSerializationexception. If the numeric value is zero, send the empty list.

 

 

 

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.