应该是“或”使用 .Net4 Hasflags: enum.HasFlag(AccessRights.Read | AccessRights.Write)

发布于 2024-12-13 03:10:27 字数 573 浏览 1 评论 0原文

我正在尝试新的 HasFlags 功能,并且想知道以下应该是否有效:

enum.HasFlag(AccessRights.Read | AccessRights.Write)

...因为它似乎没有...

 DBAccessRights rights = (DBAccessRights)permission.PermissionFlags;
  if (rights.HasFlag(DBAccessRights.WikiMode))
  {
     // works
  }


  if (rights.HasFlag(DBAccessRights.WikiMode | DBAccessRights.CreateNew))
  {
     // Doesn't work    
  }

  DBAccessRights flags = DBAccessRights.WikiMode | DBAccessRights.CreateNew;
  if (rights.HasFlag(flags))
  {
     // Doesn't work
  }

I am trying out the new HasFlags features, and was wondering if the following should work:

enum.HasFlag(AccessRights.Read | AccessRights.Write)

... because it doesn't seem to...

 DBAccessRights rights = (DBAccessRights)permission.PermissionFlags;
  if (rights.HasFlag(DBAccessRights.WikiMode))
  {
     // works
  }


  if (rights.HasFlag(DBAccessRights.WikiMode | DBAccessRights.CreateNew))
  {
     // Doesn't work    
  }

  DBAccessRights flags = DBAccessRights.WikiMode | DBAccessRights.CreateNew;
  if (rights.HasFlag(flags))
  {
     // Doesn't work
  }

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

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

发布评论

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

评论(5

旧人 2024-12-20 03:10:27

鉴于文档,我希望如果该值同时具有这些标志,则返回 true。

如果您希望它测试您的值是否具有这些标志中的任何一个,您将需要

value.HasFlag(AccessRights.Read) | value.HasFlag(AccessRights.Write)

如果这对您来说可读性不够,您可能需要查看我的无约束旋律 项目。碰巧它已经具有您想要的功能(作为 Flags.cs):

// Same as value.HasFlag(AccessRights.Read | AccessRights.Write)
value.HasAll(AccessRights.Read | AccessRights.Write)

// Same as value.HasFlag(AccessRights.Read) | value.HasFlag(AccessRights.Write)
value.HasAny(AccessRights.Read | AccessRights.Write)

这些会让事情变得更清楚,IMO。他们还会避免拳击,并且类型安全:)

Given the documentation, I'd expect that to return true if the value has both of those flags.

If you want it to test whether your value has either of those flags, you'll need

value.HasFlag(AccessRights.Read) | value.HasFlag(AccessRights.Write)

If that's not good readable enough for you, you may want to look at my Unconstrained Melody project. It so happens that that already has the functionality you want (as extension methods in Flags.cs):

// Same as value.HasFlag(AccessRights.Read | AccessRights.Write)
value.HasAll(AccessRights.Read | AccessRights.Write)

// Same as value.HasFlag(AccessRights.Read) | value.HasFlag(AccessRights.Write)
value.HasAny(AccessRights.Read | AccessRights.Write)

Those would make it clearer, IMO. They'd also avoid boxing, and be typesafe :)

紫竹語嫣☆ 2024-12-20 03:10:27

来自 MSDN

HasFlag 方法返回以下布尔值的结果
表达。

thisInstance 和 flag = flag

对于复杂的标志,例如 AccessRights.Read | AccessRights.Write,这将检查所有“包含”标志是否存在。

您可能想要检查是否存在任何标志,在这种情况下您可以执行以下操作:

myAccessRights & (AccessRights.Read | AccessRights.Write) != 0 

From MSDN:

The HasFlag method returns the result of the following Boolean
expression.

thisInstance And flag = flag

For a complex flag such as AccessRights.Read | AccessRights.Write, this will check that all the "contained" flags are present.

You probably want to check that any of the flags are present, in which case you can do:

myAccessRights & (AccessRights.Read | AccessRights.Write) != 0 
停滞 2024-12-20 03:10:27

| 运算符是按位或。这意味着如果 Read 为 1 并且 Write 为 2,则值 Read | Write 为 3(请参阅其二进制表示形式)。因此,仅当您的 enum 变量同时设置了 ReadWrite 时,HasFlag 才返回 true 。

The | operator is bitwise or. It means that if Read is 1 and Write is 2, the value Read | Write is 3 (see its binary representation). So HasFlag returns true only if your enum variable have both Read and Write set.

来日方长 2024-12-20 03:10:27

或者,您可以反转表达式的顺序:

//returns true - a bit easier on the eye
(AccessRights.Read | AccessRights.Write).HasFlag(myAccessRights)

如果您有任一,这将返回 true写访问。这在功能上相当于:

//also returns true - as per @Ani's answer
myAccessRights & (AccessRights.Read | AccessRights.Write) != 0

EDIT

正如评论中所指出的,如果 myAccessRights 为空,第一个表达式将返回 true,如果 myAccessRights 不仅仅是读取和写入,则第一个表达式将返回 false。

Alternatively, you could just reverse the order of the expression:

//returns true - a bit easier on the eye
(AccessRights.Read | AccessRights.Write).HasFlag(myAccessRights)

This will return true if you have either Read | Write access. That would be functionally equivalent to:

//also returns true - as per @Ani's answer
myAccessRights & (AccessRights.Read | AccessRights.Write) != 0

EDIT

As pointed out in the comments, The first expression will return true if myAccessRights is empty, and false if myAccessRights has more than just Read and Write.

演出会有结束 2024-12-20 03:10:27

在带标记的枚举中,您可以简单地执行以下操作:

(MyEnum01 & MyEnum02) != 0

如果有任何公共位,则会设置一个位,因此数字不再是 0。如果没有公共位,结果为 0。为此,标记为 0 的枚举应表示“无”。

这是通用枚举的静态方法和特定枚举的静态方法:

public static partial class UtilityMethods
{
    public static bool HasAnyFlags (Enum enumA, Enum enumB)
    {
        return ((int) (object) enumA & (int) (object) enumB) != 0;
    }

    public static bool HasAnyFlags (MyEnum enumA, MyEnum enumB)
    {
        return (enumA & enumB) != 0;
    }
}

您还可以创建自己的扩展方法:

public static partial class ExtensionMethods
{
    public static bool HasAnyFlag (this Enum e, Enum compared)
    {
        return ((int) (object) e & (int) (object) compared) != 0;
    }
}

对于通用枚举,我从 这里。据称,使用装箱比使用Converter.ToInt32() 性能更高。

In a flagged enum you could simply do:

(MyEnum01 & MyEnum02) != 0

If there are any common bits, a bit will be set and thus the number is no longer 0. If there are no common bits, the result is 0. For that the flagged enum for 0 should represent "none".

Here is a static method for generic enums and one for a specific one:

public static partial class UtilityMethods
{
    public static bool HasAnyFlags (Enum enumA, Enum enumB)
    {
        return ((int) (object) enumA & (int) (object) enumB) != 0;
    }

    public static bool HasAnyFlags (MyEnum enumA, MyEnum enumB)
    {
        return (enumA & enumB) != 0;
    }
}

You could also create your own extension method:

public static partial class ExtensionMethods
{
    public static bool HasAnyFlag (this Enum e, Enum compared)
    {
        return ((int) (object) e & (int) (object) compared) != 0;
    }
}

For the generic enums I took the conversion from here. Allegedly it's more performant to do it with boxing rather than Converter.ToInt32().

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