时间:2019-01-17 标签:c#enumexclusive

发布于 2024-10-24 12:48:25 字数 443 浏览 0 评论 0原文

假设我有一个像这样的枚举:

[Flags]
public enum NotificationMethodType {
    Email = 1,
    Fax = 2,
    Sms = 4
}

假设我有一个变量定义为:

NotificationMethodType types = (NotificationMethodType.Email | NotificationMethodType.Fax)

如何找出“types”变量中未定义的所有NotificationMethodType 值?换句话说:

NotificationMethodType notAssigned = NotificationMethodType <that are not> types

Let's say I have an enum like this:

[Flags]
public enum NotificationMethodType {
    Email = 1,
    Fax = 2,
    Sms = 4
}

And let's say I have a variable defined as:

NotificationMethodType types = (NotificationMethodType.Email | NotificationMethodType.Fax)

How do I figure out all of the NotificationMethodType values that are not defined in the "types" variable? In other words:

NotificationMethodType notAssigned = NotificationMethodType <that are not> types

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

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

发布评论

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

评论(3

浅听莫相离 2024-10-31 12:48:25

如果类型列表永远不会改变,您可以这样做:

NotificationMethodType allTypes = NotificationMethodType.Email |
    NotificationMethodType.Fax |
    NotificationMethodType.Sms;

NotificationMethodType notAssigned = allTypes & ~types;

~ 通过反转所有位来创建一个反转值。

定义此类枚举以至少将“allTypes”的定义保留在枚举本地的典型方法是在枚举中包含两个新名称:

[Flags]
public enum NotificationMethodType {
    None = 0,
    Email = 1,
    Fax = 2,
    Sms = 4,
    All = Email | Fax | Sms
}

注意:如果您选择添加 < code>All 值赋给枚举,请注意,如果 types 为空,您将不会得到一个可以打印为“电子邮件、传真、短信”的枚举,而是“全部”。

如果您不想手动维护 allTypes 列表,可以使用 Enum.GetValues 方法来完成:

NotificationMethodType allTypes = 0;
foreach (NotificationMethodType type in Enum.GetValues(typeof(NotificationMethodType)))
    allTypes |= type;

或者您可以使用 LINQ 执行相同操作:

NotificationMethodType allTypes = 
    Enum.GetValues(typeof(NotificationMethodType))
    .Cast<NotificationMethodType>()
    .Aggregate ((current, value) => current | value);

此构建通过将枚举的所有单独值进行或运算来得到 allType 值。

If the list of types never changes, you can do this:

NotificationMethodType allTypes = NotificationMethodType.Email |
    NotificationMethodType.Fax |
    NotificationMethodType.Sms;

NotificationMethodType notAssigned = allTypes & ~types;

The ~ creates an inverse value, by inverting all the bits.

A typical way to define such enums to at least keep the definition of "allTypes" local to the enum would be to include two new names into the enum:

[Flags]
public enum NotificationMethodType {
    None = 0,
    Email = 1,
    Fax = 2,
    Sms = 4,
    All = Email | Fax | Sms
}

Note: If you go the route of adding the All value to the enum, note that if types was empty, you would not get an enum that would print as "Email, Fax, Sms", but rather as "All".

If you don't want to manually maintain the list of allTypes, you can do it using the Enum.GetValues method:

NotificationMethodType allTypes = 0;
foreach (NotificationMethodType type in Enum.GetValues(typeof(NotificationMethodType)))
    allTypes |= type;

or you can do the same with LINQ:

NotificationMethodType allTypes = 
    Enum.GetValues(typeof(NotificationMethodType))
    .Cast<NotificationMethodType>()
    .Aggregate ((current, value) => current | value);

This builds the allType value by OR'ing together all the individual values of the enum.

窗影残 2024-10-31 12:48:25

一个简单的 XOR 就可以解决问题...

NotificationMethodType all = (NotificationMethodType.Email | NotificationMethodType.Fax | NotificationMethodType.Sms);
NotificationMethodType used = (NotificationMethodType.Email | NotificationMethodType.Fax);
NotificationMethodType unused = (all ^ used);

为了使其更简洁,请将 All 值直接添加到您的枚举定义中(显然将值设置为 7)。这样,您可以稍后将内容添加到枚举中,而不会破坏此代码

A simple XOR will do the trick...

NotificationMethodType all = (NotificationMethodType.Email | NotificationMethodType.Fax | NotificationMethodType.Sms);
NotificationMethodType used = (NotificationMethodType.Email | NotificationMethodType.Fax);
NotificationMethodType unused = (all ^ used);

To make this a little cleaner, add the All value to your enum definition directly (set value to 7 obviously). This way, you can add things to the enum later without breaking this code

假装爱人 2024-10-31 12:48:25
var notAssigned = Enum.GetValues(typeof(NotificationMethodType))
                      .Cast<NotificationMethodType>()
                      .Where(x => !types.HasFlag(x))
                      .Aggregate((a, x) => a | x);
var notAssigned = Enum.GetValues(typeof(NotificationMethodType))
                      .Cast<NotificationMethodType>()
                      .Where(x => !types.HasFlag(x))
                      .Aggregate((a, x) => a | x);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文