Should “or” work with .Net4 Hasflags: enum.HasFlag(AccessRights.Read | AccessRights.Write)

瘦欲@ 提交于 2019-12-30 04:05:48

问题


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
  }

回答1:


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 :)




回答2:


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 



回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/7984377/should-or-work-with-net4-hasflags-enum-hasflagaccessrights-read-accessrig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!