日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

在WCF中使用Flag Enumerations

發布時間:2024/10/8 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在WCF中使用Flag Enumerations 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在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:

  • Attempt to find an enumeration member (with the EnumMemberAttribute attribute applied) that maps to the numeric value. If found, send a list that contains just that member.
  • Attempt to break the numeric value into a sum such that there are enumeration members (each with the EnumMemberAttribute attribute applied) that map to each part of the sum. Send the list of all these members. Note that the greedy algorithm is 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.
  • If the preceding two steps fail, and the numeric value is nonzero, throw a SerializationException. If the numeric value is zero, send the empty list.
  • ?

    ?


    ?

    posted on 2011-11-28 10:48 DotNet編程 閱讀(...) 評論(...) 編輯 收藏

    轉載于:https://www.cnblogs.com/furenjun/archive/2011/11/28/2265896.html

    總結

    以上是生活随笔為你收集整理的在WCF中使用Flag Enumerations的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。