.NET框架中位标志的实际使用
正在研究如何通过使用 flags 属性和 bitwize 运算符装饰枚举来将其用作位标志(见下文)。
.NET 框架中是否有任何地方使用了此模式?我喜欢这个,但想看看更多现实生活中的例子
[Flags]
enum Days2 : int
{
None = 0x0,
Sunday = 0x1,
Monday = 0x2,
Tuesday = 0x4,
Wednesday = 0x8,
Thursday = 0x10,
Friday = 0x20,
Saturday = 0x40
}
Days2 meetingDays = Days2.Tuesday | Days2.Thursday;
// Set an additional flag using bitwise OR.
meetingDays = meetingDays | Days2.Friday;
Console.WriteLine("Meeting days are {0}", meetingDays);
Was looking at how enums can be used as bit flags by decorating them with the flags attribute and bitwize operators (see below).
Are there any places in the .NET framework that this pattern is used? I like this but want to see some more real life examples
[Flags]
enum Days2 : int
{
None = 0x0,
Sunday = 0x1,
Monday = 0x2,
Tuesday = 0x4,
Wednesday = 0x8,
Thursday = 0x10,
Friday = 0x20,
Saturday = 0x40
}
Days2 meetingDays = Days2.Tuesday | Days2.Thursday;
// Set an additional flag using bitwise OR.
meetingDays = meetingDays | Days2.Friday;
Console.WriteLine("Meeting days are {0}", meetingDays);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的 - 查看
MethodBase.Attributes
例如,说明成员是否是公共的、静态的等。<代码>FileAccess 和
FileOptions
也是基于文件的示例。如果您打开反射器,找到
FlagsAttribute
,然后点击“分析”(Ctrl-R) 并展开“使用者”,您将看到大量使用它的类型。不过需要一段时间:)Yes - look at
MethodBase.Attributes
for example, saying whether a member is public, static etc.FileAccess
andFileOptions
are file-based examples, too.If you open reflector, find
FlagsAttribute
and then hit "Analyze" (Ctrl-R) and expand "used by" you'll see loads of types using it. It takes a while though :)您将看到的最常见的 [Flags] 枚举之一是正则表达式选项枚举。这是一个示例:
更多信息:http://msdn .microsoft.com/en-us/library/system.text.regularexpressions.regexoptions.aspx
几年前,我写了一篇关于在 .Net 中使用位枚举的教程,也许这会有所帮助:
http://www.johnsample.com/articles/BitwiseEnums.aspx
One of the most common [Flags] enums you'll see is the regex options enum. Here is an example:
More here: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regexoptions.aspx
Years ago I wrote a tutorial on using bit enums in .Net, maybe this will help:
http://www.johnsample.com/articles/BitwiseEnums.aspx
有很多地方都使用
FlagsAttribute
用于 BCL 中的枚举。下面是一个示例 -
BindingFlags
反射代码中使用的参数。There are many places where the
FlagsAttribute
is used for enumrations in the BCL.Here is one example - the
BindingFlags
parameter used in reflection code.它有很多地方被使用,它在 .NET 框架中的文件属性中使用,我见过它用作安全标志,即
它们基本上非常适合您可以选择某些内容的可能属性的任何场景,如果您例如,可以选择颜色或选择种族等。
另一个常见的地方是 winforms 对话框,它们具有由它们组成的属性,您可以将它们组合在一起作为消息框来决定您选择的按钮好吧|取消 |忽略等
There are tons of places it's used, it's used in the .NET framework in file attributes, I've seen it used as security flags i.e.
They're basically great for any scenario where you have a selection of possible attributes for something, if you have something that could be a selection of colors for example or a selection of ethnicitys or etc.
Another common place to see them is winforms dialogs have attributes made up of them, you can or them together for a messagebox to decide the selection of buttons you get like Ok | Cancel | Ignore etc.
mscorlib 和 System 程序集中有大量示例,但在日常使用中会遇到多少是一个比较棘手的问题。
例如,System.Threading.ThreadState 是当前线程状态和待处理请求的组合。
实际上,我转储了 System 和 mscorlib 中的所有标志枚举,只是为了咯咯笑,然后去寻找有趣的:
There are plenty of examples in the mscorlib and System assemblies, although how many you'll encounter in everyday use is a trickier question.
For instance, System.Threading.ThreadState is a combination of the current thread status, and pending requests.
I actually dumped out all of the flag enums in System and mscorlib, just for giggles, and then went searching for interesting ones:
设置字体样式选项时怎么样,例如:
How about when setting font style options, such as :