有条件地设置标志枚举

Set flags enum conditionally

我有一个枚举定义如下

 [Flags]
public enum Roles
{
    [Descriptor("owner", "TheProficientLab representative")]
    OWNER = 0,

    [Descriptor("administrator", "Sets up users, questions, modules and reviews training and competency")]
    ADMIN = 1,

    [Descriptor("trainer", "Completes and submits training session with trainee")]
    TRAINER = 2,

    [Descriptor("trainee", "Completes training session with trainer")]
    TRAINEE = 3,

    [Descriptor("assessor", "Evaluates and submits competency assessments")]
    ASSESSOR = 4,

    [Descriptor("director or designee", "Approves training and competency")]
    DIRECTOR = 5
}

然后是属性中的class

public Common.Constants.Role.Roles Roles { get; set; }

我如何根据条件将该枚举设置为多个标志

基本上我正在寻找这样的东西

if (something)
   Item.Roles += Roles.ADMIN
if (something else)
   Item.Roles += Roles.ASSESSOR

按位世界中是否有 +=-= 等价物?

根据 Idle_Mind 在评论中的回答

Item.Roles |= Role.ADMIN 添加

ItemRoles &= ~Role.ADMIN 删除

并且值必须是 2 的幂 docs