【问题标题】:How to do flags in C#如何在 C# 中做标志
【发布时间】:2011-11-23 22:54:39
【问题描述】:

在 C# 中,我之前看到过以标志格式使用的枚举。比如Regex 对象。

Regex regex = new Regex("expression", RegexOptions.Something | RegexOptions.SomethingElse);

如果我有自定义枚举:

enum DisplayType
{
    Normal,
    Inverted,
    Italics,
    Bold
}

我将如何格式化一个方法来接受一个参数的多个枚举,例如 Regex 的语法?即SomeMethod(DisplayType.Normal | DisplayType.Italics);

【问题讨论】:

    标签: c# .net asp.net methods enums


    【解决方案1】:

    我知道我参加聚会有点晚了,但我想添加我保留的这段代码 sn-p,因为它可以帮助我在需要时创建标记的枚举。

    [Flags]
    enum Generic32BitFlags
    {
        None  =            0,       /* 00000000000000000000000000000000 */
        Bit1  =            1 &lt&lt 1,  /* 00000000000000000000000000000001 */
        Bit2  =            1 &lt&lt 2,  /* 00000000000000000000000000000010 */
        Bit3  =            1 &lt&lt 3,  /* 00000000000000000000000000000100 */
        Bit4  =            1 &lt&lt 4,  /* 00000000000000000000000000001000 */
        Bit5  =            1 &lt&lt 5,  /* 00000000000000000000000000010000 */
        Bit6  =            1 &lt&lt 6,  /* 00000000000000000000000000100000 */
        Bit7  =            1 &lt&lt 7,  /* 00000000000000000000000001000000 */
        Bit8  =            1 &lt&lt 8,  /* 00000000000000000000000010000000 */
        Bit9  =            1 &lt&lt 9,  /* 00000000000000000000000100000000 */
        Bit10 =            1 &lt&lt 10, /* 00000000000000000000001000000000 */
        Bit11 =            1 &lt&lt 11, /* 00000000000000000000010000000000 */
        Bit12 =            1 &lt&lt 12, /* 00000000000000000000100000000000 */
        Bit13 =            1 &lt&lt 13, /* 00000000000000000001000000000000 */
        Bit14 =            1 &lt&lt 14, /* 00000000000000000010000000000000 */
        Bit15 =            1 &lt&lt 15, /* 00000000000000000100000000000000 */
        Bit16 =            1 &lt&lt 16, /* 00000000000000001000000000000000 */
        Bit17 =            1 &lt&lt 17, /* 00000000000000010000000000000000 */
        Bit18 =            1 &lt&lt 18, /* 00000000000000100000000000000000 */
        Bit19 =            1 &lt&lt 19, /* 00000000000001000000000000000000 */
        Bit20 =            1 &lt&lt 20, /* 00000000000010000000000000000000 */
        Bit21 =            1 &lt&lt 21, /* 00000000000100000000000000000000 */
        Bit22 =            1 &lt&lt 22, /* 00000000001000000000000000000000 */
        Bit23 =            1 &lt&lt 23, /* 00000000010000000000000000000000 */
        Bit24 =            1 &lt&lt 24, /* 00000000100000000000000000000000 */
        Bit25 =            1 &lt&lt 25, /* 00000001000000000000000000000000 */
        Bit26 =            1 &lt&lt 26, /* 00000010000000000000000000000000 */
        Bit27 =            1 &lt&lt 27, /* 00000100000000000000000000000000 */
        Bit28 =            1 &lt&lt 28, /* 00001000000000000000000000000000 */
        Bit29 =            1 &lt&lt 29, /* 00010000000000000000000000000000 */
        Bit30 =            1 &lt&lt 30, /* 00100000000000000000000000000000 */
        Bit31 =            1 &lt&lt 31, /* 01000000000000000000000000000000 */
        Bit32 =            1 &lt&lt 32, /* 10000000000000000000000000000000 */
    }

    【讨论】:

      【解决方案2】:

      使用 FlagsAttribute,像这样

      [Flags]
      enum DisplayType
      {
          None = 0x0,
          Normal = 0x1,
          Inverted = 0x2,
          Italics = 0x4,
          Bold = 0x8
      }
      

      【讨论】:

      • 指南建议您使用 None 作为值 0。
      【解决方案3】:

      请参阅此处的“枚举类型作为位标志”:http://msdn.microsoft.com/en-us/library/cc138362.aspx

      【讨论】:

        【解决方案4】:

        使用FlagsAttribute。 MSDN 文档解释了一切。没有必要在这里重复。

        对于你的例子,你会说:

        [Flags]
        enum DisplayType {
            None = 0,
            Normal = 1,
            Inverted = 2,
            Italics = 4,
            Bold = 8
        }
        

        【讨论】:

        • @Jonas:是的。他甚至不知道去查FlagsAttribute。现在他做到了。鉴于该指针,没有必要重复该文档中的所有内容。如果他对其中一项准则有具体问题,他应该就此提出一个新问题。您真的对此投反对票吗?
        猜你喜欢
        • 1970-01-01
        • 2011-07-21
        • 1970-01-01
        • 1970-01-01
        • 2018-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多