zz flag attribute for enum

http://blog.sina.com.cn/s/blog_429fe72e010006u3.html

[SerializableAttribute] [AttributeUsageAttribute(AttributeTargets.Enum, Inherited = false)] [ComVisibleAttribute(true)] public class FlagsAttribute : Attribute

位域一般用于由可组合出现的元素组成的列表,而枚举常数一般用于由互相排斥的元素组成的列表。所以,位域设计为经过按位“或”运算组合来生成未命名的值,而枚举常数则不是。语言在对位域的使用和对枚举常数的使用上不一样。html

FlagsAttribute 的属性

AttributeUsageAttribute 应用于此类,其 Inherited 属性指定 false。此属性只能应用于枚举。安全

用于 FlagsAttribute 和 Enum 的准则

  • 只有要对数值执行按位运算(AND、OR、XOR)时才对枚举使用 FlagsAttribute 自定义属性。测试

  • 用 2 的幂(即 一、二、四、8 等)定义枚举常量。这意味着组合的枚举常量中的各个标志都不重叠。spa

  • 请考虑为经常使用标志组合建立一个枚举常量。例如,若是用于文件 I/O 操做的枚举包含枚举常量 Read = 1和 Write = 2,请考虑建立枚举常量 ReadWrite = Read OR Write,该常量组合了 Read 和 Write 标志。此外,在某些状况下,可能会将用于组合标志的按位 OR 运算视为一种高级概念,在简单任务中不须要执行此操做。线程

  • 将负数定义为标志枚举常量时应谨慎,由于不少标志位置均可能设置为 1,这可能使您的代码产生混淆并易于发生代码错误。设计

  • 测试数值中是否已设置标志的一种简便方法为:在数值和标志枚举常量之间执行按位“与”操做,这种方法会将数值中与标志不对应的全部位都设置为零,而后测试该操做的结果是否等于该标志枚举常量。htm

  • 将 None 用做值为零的标志枚举常量的名称。在按位 AND 运算中,不能使用 None 枚举常量测试标志,由于所得的结果始终为零。可是,您能够在数值与 None 枚举常量之间执行逻辑(不是按位)比较,以肯定数值中是否已设置任何位。blog

    若是建立的是值枚举而不是标志枚举,建立 None 枚举常量仍十分有用。缘由是在默认状况下,公共语言运行库会将用于枚举的内存初始化为零。所以,若是不定义值为零的常量,则枚举在建立时将包含非法值。继承

    若是明显存在应用程序须要表示的默认状况,请考虑使用值为零的枚举常量表示默认值。若是不存在默认状况,请考虑使用值为零的枚举常量(这意味着该状况不禁任何其余枚举常量表示)。内存

  • 不要仅为了反映枚举自身的状态而定义枚举值。例如,不要定义仅用于标记枚举末尾的枚举常量。若是须要肯定枚举的最后一个值,请显式检查该值。此外,若是枚举常量范围中的全部值都有效,还能够对第一个和最后一个枚举常量执行范围检查。

  • 不要指定保留供未来使用的枚举常量。

  • 在定义采用枚举常量做为值的方法或属性时,应考虑对该值进行验证。缘由是即便没有在枚举中定义某个数值,也能够将该数值强制转换为枚举类型。

示例示例

下面的代码示例阐释了如何使用 FlagsAttribute 属性,并显示了使用 Enum 声明的 FlagsAttribute 在ToString 方法上的效果。

// Example of the FlagsAttribute attribute.
using System;

class FlagsAttributeDemo
{
    // Define an Enum without FlagsAttribute.
    enum SingleHue : short
    {
        Black = 0,
        Red = 1,
        Green = 2,
        Blue = 4
    };

    // Define an Enum with FlagsAttribute.
    [FlagsAttribute]
    enum MultiHue : short
    {
        Black = 0,
        Red = 1,
        Green = 2,
        Blue = 4
    };

    static void Main( )
    {
        Console.WriteLine(
            "This example of the FlagsAttribute attribute \n" +
            "generates the following output." );
        Console.WriteLine(
            "\nAll possible combinations of values of an \n" +
            "Enum without FlagsAttribute:\n" );
       
        // Display all possible combinations of values.
        for( int val = 0; val <= 8; val++ )
            Console.WriteLine( "{0,3} - {1}",
                val, ( (SingleHue)val ).ToString( ) );

        Console.WriteLine(
            "\nAll possible combinations of values of an \n" +
            "Enum with FlagsAttribute:\n" );
       
        // Display all possible combinations of values.
        // Also display an invalid value.
        for( int val = 0; val <= 8; val++ )
            Console.WriteLine( "{0,3} - {1}",
                val, ( (MultiHue)val ).ToString( ) );
    }
}

/*
This example of the FlagsAttribute attribute
generates the following output.

All possible combinations of values of an
Enum without FlagsAttribute:

  0 - Black
  1 - Red
  2 - Green
  3 - 3
  4 - Blue
  5 - 5
  6 - 6
  7 - 7
  8 - 8

All possible combinations of values of an
Enum with FlagsAttribute:

  0 - Black
  1 - Red
  2 - Green
  3 - Red, Green
  4 - Blue
  5 - Red, Blue
  6 - Green, Blue
  7 - Red, Green, Blue
  8 - 8
*/

继承层次结构继承层次结构

System.Object
    System.Attribute
     System.FlagsAttribute
线程安全线程安全
此类型的任何公共静态(Visual Basic 中的  Shared)成员都是线程安全的,但不保证全部实例成员都是线程安全的。
平台平台

Windows 9八、Windows 2000 SP四、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 200三、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP二、Windows XP Starter Edition

.NET Framework 并非对每一个平台的全部版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息版本信息

.NET Framework

受如下版本支持:2.0、1.一、1.0

.NET Compact Framework

受如下版本支持:2.0、1.0
相关文章
相关标签/搜索