有条件地设置标志

发布于 2025-01-24 10:48:12 字数 913 浏览 2 评论 0原文

我的枚举定义为如下,

 [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
}

然后是类

public common.constants.role.orles角色{get;放; }

我将如何将枚举设置为基于条件的多个标志

我正在寻找类似的东西。

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

, 世界?

I have an enum defined as follows

 [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
}

It is then a property in a class

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

How would I set that enum to multiple flags based on condition

Essentially I am looking for something like this

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

Is there a += and -= equivalents in bitwise world?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-黛色若梦 2025-01-31 10:48:12

在注释> code

> item.Roles | roun.admin 中per indle_mind的答案添加

iteg> itemroles& =〜prole.Admin要删除

,并且值必须是每个幂的幂 docs

Per Idle_Mind's answer in the comments

Item.Roles |= Role.ADMIN to add

ItemRoles &= ~Role.ADMIN to remove

and the values need to be powers of 2 per docs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文