c# - 确定枚举中缺少哪些标志

标签 c# .net

假设我有下面的枚举:

[Flags]
public enum NotifyType
{

    None = 0,
    Window = 1 << 0,
    Sound = 1 << 1,
    Flash = 1 << 2,
    MobileSound = 1 << 3,
    MobilePush = 1 << 4
}

考虑两个枚举:

var myenums = Window | Sound | Flash;


//var possibleUpdate = Window | MobilePush;

void UpdateMyEnums(NotifyType possibleUpdate)
{
    //Does myenums contain all the flags in 'possibleUpdate'?  If not add
    //the missing flags to myenums

}

possibleUpdate 相比,如何确定 myenums 变量不包含 NotifyType.MobilePush 值?我是否必须针对 myenums 测试 possibleUpdate 中的每个标志?

我在 .NET 4.0 上使用 C#

最佳答案

if (myenums & possibleUpdate != possibleUpdate)
    //not a possible update

要获取 myenums 中不需要的标志:

NotifyType missing = (~(myenums ^ wanted) ^ wanted) & (myenums | wanted);

关于c# - 确定枚举中缺少哪些标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18323031/

相关文章:

C# Dll 导入失败 : "The application has failed to start because its side-by-side configuration is incorrect"

c# - 如何部署 "Release"版本的 ASP.NET MVC5 应用程序

c# - Metro 应用程序中像素字节数组的位图(win 8)

c# - 在线程池线程的 Windows Phone 7 上调整图像大小?

.net - 是否可以使用 WCF 服务而不是 WCF 数据服务来实现服务器端分页

c# - 覆盖基类的实现,使得从继承的基类函数调用也调用被覆盖的函数

c# - 在同一张表的主键上创建外键约束的不良做法?

c# - WCF:回调不是异步的

C# 使用多线程或并行执行执行 SQL SP

.net - 从 .net 代码更改音量