如何在C#中做标志
在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
FlagsAttribute
。 MSDN 文档解释了一切。没有必要在这里重复。对于你的例子,你会说:
Use the
FlagsAttribute
. The MSDN documentation explains everything. No point repeating it here.For your example, you'd say:
使用 FlagsAttribute,就像这样
Use the FlagsAttribute, like so
我知道我参加聚会有点晚了,但我想添加我保留的这段代码片段,因为它可以帮助我在需要时创建标记枚举。
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.
请参阅此处的“作为位标志的枚举类型”: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