有条件地设置标志
我的枚举定义为如下,
[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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在注释> 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 addItemRoles &= ~Role.ADMIN
to removeand the values need to be powers of 2 per docs