在WCF中使用Flag Enumerations
請看MSDN示例:
?
[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
}
?
注意以下幾點:
1. 請使用[Flags]標志。
2.所有應用了EnumMemberAttribute的枚舉成員值必須是不間斷的2的冪 (如 1, 2, 4, 8, 16, 32, 64).
3.如果通過數值來找枚舉成員(比如通過4 來找PowerDoors),會先判斷是否存在這個成員,不存在則判斷是否存在這樣的組合成員,如果仍然不存在且數值不為0的話則會拋出SerializationException,如果數值為0則返回空列表。
4.未標記為[EnumMember]的成員,在WCF客戶端不能使用,如上例中的None?=?0。
詳細用法請見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
Applying the EnumMemberAttribute attribute to simple enumerations has no effect.
It makes no difference whether or not the SerializableAttribute attribute is applied to the enumeration.
The fact that the DataContractSerializer class honors the NonSerializedAttribute attribute applied to enumeration members is different from the behavior of the BinaryFormatter and the SoapFormatter. Both of those serializers ignore the NonSerializedAttribute attribute.
Flag Enumerations
You can apply the FlagsAttribute attribute to enumerations. In that case, a list of zero or more enumeration values can be sent or received simultaneously.
To do so, apply the DataContractAttribute attribute to the flag enumeration and then mark all the members that are powers of two with the EnumMemberAttribute attribute. 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:
?
?
?
轉載于:https://www.cnblogs.com/furenjun/archive/2011/11/28/2265896.html
總結
以上是生活随笔為你收集整理的在WCF中使用Flag Enumerations的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android自定义View研究(四)
- 下一篇: 模拟器上安装不能被卸载的apk