【问题标题】:Append Enum Flags to a Parameter in a Loop (Bitwise Appending)将枚举标志附加到循环中的参数(按位附加)
【发布时间】:2011-11-29 21:00:51
【问题描述】:

在 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());
}

从概念上讲,这就是我想要的,但我不能将循环中的值“+=”到参数。

【问题讨论】:

    标签: c# bit-manipulation enum-flags


    【解决方案1】:

    你不需要分裂。如果您在枚举定义上使用 [Flags] 属性,Enum.Parse 能够解析多个值,您这样做了。只需解析并使用 |= 运算符来“添加”标志。

    string protectionOptionsString = "NoPrevention, PreventPrinting";
    myObj.Protection |= (ProtectionOptions)Enum.Parse(typeof(ProtectionOptions), protectionOptionsString);
    

    【讨论】:

    • 完美……那行得通。谢谢!给你两个快速的问题。一,出于好奇,如果我想在不使用内置 Parse 函数的情况下“附加”标志,这可能吗?第二,对于我的 Enum,我听说过关于是否应该使用 0 值并为其命名的相互矛盾的故事。如果我确实使用 0 值,它应该是“无”还是可以是任何值?
    • 如果值表示为字符串,则必须使用 Parse 转换为枚举实例。除非它是无,否则不应使用 0。问题是,如果使用您的枚举的程序员检查值,他们有时会使用 0 进行比较(他们不应该这样做,但这很常见)。如果 0 是一个值而不是缺少值,那么 if ((currentWeather & Cloudy) != 0) 当 Cloudy 定义为 0 时测试标志是否有 Cloudy 将返回 false 而不是预期的 true。我在教程中详细提到了这一点。按照我在答案中指定的链接。
    【解决方案2】:

    使用|= 运算符。

    myObj.Protection |= (ProtectionOptions) Enum.Parse(typeof (ProtectionOptions), protectionOption.Trim());
    

    【讨论】:

      【解决方案3】:

      你为什么不能这样做?

      string protectionOptionsString = "NoPrevention, PreventPrinting";
      string[] protectionOptions = protectionOptionsString.Split(',');
      foreach (string protectionOption in protectionOptions)
      {
        myObj.Protection |= (ProtectionOptions) Enum.Parse(typeof (ProtectionOptions), protectionOption.Trim());
      }
      

      或者,您可以存储与枚举 ((Int32)myObj.Protection) 等效的整数并将其加载为整数。

      【讨论】:

      • 感谢 Zelda,这回答了我发布给 Motti 的第一个问题。我接受了他的回答,因为它现在更符合我的需求,但这清楚地表明我需要使用“|=”而不是“+=”来追加。我很感激!
      【解决方案4】:

      您想使用compound OR assignment operator

      string protectionOptionsString = "NoPrevention, PreventPrinting";
      string[] protectionOptions = protectionOptionsString.Split(',');
      
      foreach (string protectionOption in protectionOptions)
      {
        myObj.Protection |= (ProtectionOptions)Enum.Parse(typeof(ProtectionOptions), 
                                                          protectionOption.Trim());
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-24
        • 1970-01-01
        • 2013-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多