如何在C#中做标志

发布于 2024-12-06 14:56:39 字数 403 浏览 5 评论 0原文

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

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

如果我有一个自定义枚举:

enum DisplayType
{
    Normal,
    Inverted,
    Italics,
    Bold
}

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

In C# I have seen enums used in a flag format before. Such as with the Regex object.

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

If I have a custom enum:

enum DisplayType
{
    Normal,
    Inverted,
    Italics,
    Bold
}

How would i format a method to accept multiple enums for one argument such as the syntax for Regex? i.e SomeMethod(DisplayType.Normal | DisplayType.Italics);.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深海不蓝 2024-12-13 14:56:39

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

对于你的例子,你会说:

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

Use the FlagsAttribute. The MSDN documentation explains everything. No point repeating it here.

For your example, you'd say:

[Flags]
enum DisplayType {
    None = 0,
    Normal = 1,
    Inverted = 2,
    Italics = 4,
    Bold = 8
}
你列表最软的妹 2024-12-13 14:56:39

使用 FlagsAttribute,就像这样

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

Use the FlagsAttribute, like so

[Flags]
enum DisplayType
{
    None = 0x0,
    Normal = 0x1,
    Inverted = 0x2,
    Italics = 0x4,
    Bold = 0x8
}
感情洁癖 2024-12-13 14:56:39

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

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

I know i'm a bit late to the party but I would like to add I this code snippet i keep around because it helps me create Flagged enums when i need them.

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

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

See "Enumeration Types as Bit Flags" here: http://msdn.microsoft.com/en-us/library/cc138362.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文