Combining Enum Values with Bit-Flags

后端 未结 4 1578
傲寒
傲寒 2021-02-13 04:43

I have this scenario where user has its role

NormalUser
Custodian
Finance

both Custodian and Finance is a Super

4条回答
  •  春和景丽
    2021-02-13 05:22

    I think this might be a duplicate of How do you pass multiple enum values in C#?

    Where the & bitmask can do the trick.

    ((Role.NormalUser & Role.All) == Role.NormalUser)
    

    Inspecting this closer you will get the following:

    0b0 & 0b11 == 0b0
    

    However if you lets say want to check if the SuperUser is in finance you will get the following:

    ((Role.SuperUser & Role.Finance) == Role.Finance)
    

    This will evaluate to:

    0b11 & 0b10 == 0b10
    

提交回复
热议问题