欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 编程资源 > 编程问答 >内容正文

编程问答

在wcf中使用flag enumerations -凯发k8官方网

发布时间:2024/10/8 编程问答 0 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 在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

    总结

    以上是凯发k8官方网为你收集整理的在wcf中使用flag enumerations的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

    网站地图