在 C# 中,我试图将值“添加”到接受枚举标志的参数中。我可以使用按位运算符“|”在一行上执行此操作,但我似乎无法在循环中附加到参数。
我将以下枚举指定为标志。
[Flags]
public enum ProtectionOptions
{
NoPrevention = 0,
PreventEverything = 1,
PreventCopying = 2,
PreventPrinting = 4,
PrintOnlyLowResolution = 8
}
现在,我可以轻松地使用以下代码向参数添加标志值:
myObj.Protection = ProtectionOptions.PreventEverything | ProtectionOptions.PrintOnlyLowResolution;
但是,我想做的是从 CSV 字符串(来自 Web.Config)中获取保护选项列表,遍历它们并将它们添加到我的 myObj.ProtectionOptions 属性中。我不知道如何在不使用按位或“|”的情况下循环执行此操作 操作员。这是我想做的事情:
string protectionOptionsString = "NoPrevention, PreventPrinting";
string[] protectionOptions = protectionOptionsString.Split(',');
foreach (string protectionOption in protectionOptions)
{
myObj.Protection += (ProtectionOptions) Enum.Parse(typeof (ProtectionOptions), protectionOption.Trim());
}
从概念上讲,这就是我想要的,但我不能将循环中的值“+=”传递给参数。