Combining Enum Values with Bit-Flags

后端 未结 4 1569
傲寒
傲寒 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:29

    You can add the Flag-attribute to the Enum

    [Flags]
    public enum Role
    {
        NormalUser,
        Custodian,
        Finance,
        SuperUser = Custodian | Finance,
        All = Custodian | Finance | NormalUser
    }
    

    Then you can check for a role with this expression:

     Role testRole = Role.Finance
     if(testRole & Role.SuperUser == Role.SuperUser){
          //testRole is SuperUser
     }
    

提交回复
热议问题