我有以下代码,我想知道是否有办法在最后一部分(仅复制文件)中编写按位运算符,将其设置为 All 值,然后在一行中删除另外两个。

private void cbInstallType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cbInstallType.Text == "Full Install")
        {
            eventFlags = GBFEvents.All;
        }

        else if (cbInstallType.Text == "DB Only")
        {
            eventFlags = (GBFEvents.InitGBFSQL | GBFEvents.PerformDatabaseUpdate);
        }

        else if (cbInstallType.Text == "Copy Files Only")
        {
            eventFlags = GBFEvents.All;
            eventFlags &= ~(GBFEvents.InitGBFSQL | GBFEvents.PerformDatabaseUpdate);
        }
    }

GBFEvents 是这样定义的:
    public enum GBFEvents
{
    NONE                        =       0,
    InitGBFSQL                  =       1 << 0,
    ServiceIISControlDown       =       1 << 1,
    SetWebConfigValues          =       1 << 2,
    ReadFilelists               =       1 << 3,
    CopyFiles                   =       1 << 4,
    FixWebConfigValues          =       1 << 5,
    BuildAppPaths               =       1 << 6,
    PerformDatabaseUpdate       =       1 << 7,
    ServiceIISControlUp         =       1 << 8,
    All                         =    ~(-1 << 9)
}

最佳答案

将其更改为单行是一个非常简单的案例,只需扩展其功能即可。所以一步一步来:

var eventFlags = GBFEvents.All;
eventFlags &= ~(GBFEvents.InitGBFSQL | GBFEvents.PerformDatabaseUpdate);

如果我们扩展 &= 我们得到:
var eventFlags = GBFEvents.All;
eventFlags = eventFlags & ~(GBFEvents.InitGBFSQL | GBFEvents.PerformDatabaseUpdate);

如果我们在第二行内联 eventFlags 的使用,我们将得到:
var eventFlags = GBFEvents.All & ~(GBFEvents.InitGBFSQL | GBFEvents.PerformDatabaseUpdate);

关于c# - 从 .All 中按位减法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52892981/

10-14 05:35