我有以下代码,我想知道是否有办法在最后一节(仅复制文件)中编写按位运算符,以包括将其设置为All值,然后在一行中删除其他两个. private void cbInstallType_SelectedIndexChanged(object sender, Event
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);