.NET框架中位标志的实际使用

发布于 2024-09-13 20:42:13 字数 506 浏览 14 评论 0原文

正在研究如何通过使用 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 技术交流群。

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

发布评论

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

评论(6

如梦亦如幻 2024-09-20 20:42:13

是的 - 查看 MethodBase.Attributes 例如,说明成员是否是公共的、静态的等。

<代码>FileAccessFileOptions 也是基于文件的示例。

如果您打开反射器,找到 FlagsAttribute,然后点击“分析”(Ctrl-R) 并展开“使用者”,您将看到大量使用它的类型。不过需要一段时间:)

Yes - look at MethodBase.Attributes for example, saying whether a member is public, static etc.

FileAccess and FileOptions 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 :)

世界和平 2024-09-20 20:42:13

您将看到的最常见的 [Flags] 枚举之一是正则表达式选项枚举。这是一个示例:

Regex rxInsensitive = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
          RegexOptions.Compiled | RegexOptions.IgnoreCase);

更多信息: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:

Regex rxInsensitive = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
          RegexOptions.Compiled | RegexOptions.IgnoreCase);

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

差↓一点笑了 2024-09-20 20:42:13

有很多地方都使用 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.

余生共白头 2024-09-20 20:42:13

它有很多地方被使用,它在 .NET 框架中的文件属性中使用,我见过它用作安全标志,即

enum Roles
{
    None = 0,
    User = 1,
    Admin = 2,
    Superman = 4
}

if (_user.Roles & Roles.Admin != Roles.None) { user-is-admin }
if (_user.Roles & Roles.Superman != Roles.None) { user-is-also-superman }

它们基本上非常适合您可以选择某些内容的可能属性的任何场景,如果您例如,可以选择颜色或选择种族等。

另一个常见的地方是 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.

enum Roles
{
    None = 0,
    User = 1,
    Admin = 2,
    Superman = 4
}

if (_user.Roles & Roles.Admin != Roles.None) { user-is-admin }
if (_user.Roles & Roles.Superman != Roles.None) { user-is-also-superman }

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.

不必你懂 2024-09-20 20:42:13

mscorlib 和 System 程序集中有大量示例,但在日常使用中会遇到多少是一个比较棘手的问题。

例如,System.Threading.ThreadState 是当前线程状态和待处理请求的组合。

实际上,我转储了 System 和 mscorlib 中的所有标志枚举,只是为了咯咯笑,然后去寻找有趣的:

    Dim types = (From t In Reflection.Assembly.GetAssembly(GetType(Int32)).GetTypes() Select t).Concat( _
                (From t In Reflection.Assembly.GetAssembly(GetType(Uri)).GetTypes() Select t))

    For Each t As Type In types
        If t.IsEnum AndAlso (From att In t.GetCustomAttributes(True) Where TypeOf (att) Is FlagsAttribute).Any() Then
            Console.WriteLine("Flag Enum: {0}", t.ToString())
        End If
    Next
    Console.ReadLine()

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:

    Dim types = (From t In Reflection.Assembly.GetAssembly(GetType(Int32)).GetTypes() Select t).Concat( _
                (From t In Reflection.Assembly.GetAssembly(GetType(Uri)).GetTypes() Select t))

    For Each t As Type In types
        If t.IsEnum AndAlso (From att In t.GetCustomAttributes(True) Where TypeOf (att) Is FlagsAttribute).Any() Then
            Console.WriteLine("Flag Enum: {0}", t.ToString())
        End If
    Next
    Console.ReadLine()
疯到世界奔溃 2024-09-20 20:42:13

设置字体样式选项时怎么样,例如:

this.Font = new Font(this.Font, FontStyle.Bold | FontStyle.Italic);

How about when setting font style options, such as :

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