应该是“或”使用 .Net4 Hasflags: enum.HasFlag(AccessRights.Read | AccessRights.Write)
我正在尝试新的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
鉴于文档,我希望如果该值同时具有这些标志,则返回 true。
如果您希望它测试您的值是否具有这些标志中的任何一个,您将需要
如果这对您来说可读性不够,您可能需要查看我的无约束旋律 项目。碰巧它已经具有您想要的功能(作为
Flags.cs
):这些会让事情变得更清楚,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
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
):Those would make it clearer, IMO. They'd also avoid boxing, and be typesafe :)
来自 MSDN:
对于复杂的标志,例如 AccessRights.Read | AccessRights.Write,这将检查所有“包含”标志是否存在。
您可能想要检查是否存在任何标志,在这种情况下您可以执行以下操作:
From MSDN:
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:
|
运算符是按位或。这意味着如果Read
为 1 并且Write
为 2,则值Read | Write
为 3(请参阅其二进制表示形式)。因此,仅当您的enum
变量同时设置了Read
和Write
时,HasFlag
才返回 true 。The
|
operator is bitwise or. It means that ifRead
is 1 andWrite
is 2, the valueRead | Write
is 3 (see its binary representation). SoHasFlag
returns true only if yourenum
variable have bothRead
andWrite
set.或者,您可以反转表达式的顺序:
如果您有任一,这将返回 true写访问。这在功能上相当于:
EDIT
正如评论中所指出的,如果 myAccessRights 为空,第一个表达式将返回 true,如果 myAccessRights 不仅仅是读取和写入,则第一个表达式将返回 false。
Alternatively, you could just reverse the order of the expression:
This will return true if you have either Read | Write access. That would be functionally equivalent to:
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.
在带标记的枚举中,您可以简单地执行以下操作:
如果有任何公共位,则会设置一个位,因此数字不再是 0。如果没有公共位,结果为 0。为此,标记为 0 的枚举应表示“无”。
这是通用枚举的静态方法和特定枚举的静态方法:
您还可以创建自己的扩展方法:
对于通用枚举,我从 这里。据称,使用装箱比使用Converter.ToInt32() 性能更高。
In a flagged enum you could simply do:
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:
You could also create your own extension method:
For the generic enums I took the conversion from here. Allegedly it's more performant to do it with boxing rather than
Converter.ToInt32()
.