简体   繁体   English

具有相同值的多个枚举

[英]Multiple enums with the same values

I have a set of constants for which I use an enum in my C# project. 我有一组常量,在C#项目中使用一个枚举。 In some situations I need just a single value in other situations I need multiple values. 在某些情况下,我只需要一个值,在其他情况下,我需要多个值。 I started out with using IEnumerable or a simple array of my enum type to handle multiple values. 我首先使用IEnumerable或枚举类型的简单数组来处理多个值。 However I have recently come to need multiple values in an attribute constructor for multiple attributes which I would like to put in a const field of the class. 但是,最近我开始需要在属性构造函数中为多个属性添加多个值,这些属性要放在类的const字段中。

A simple example: 一个简单的例子:

public enum Browsers {
  Firefox, Chrome, IE, Opera, Safari
}

public class SomeClass {
  [Attr(new Browsers[] { Browsers.Firefox, Browsers.Chrome })]
  public void SomeMethod() { }

  [Attr(new Browsers[] { Browsers.Firefox, Browsers.Chrome })]
  public void SomeOtherMethod() { }
}

I would like to refactor the array of browsers out into a field in the class, however for a field to be used in an attribute constructor it must be a const field and a field with an array initialization cannot be const. 我想将浏览器数组重构为类中的一个字段,但是要在属性构造函数中使用的字段必须是const字段,而具有数组初始化的字段不能是const。

I then tried to use the FlagsAttribute on my enum so that the enum can hold multiple values. 然后,我尝试在我的枚举上使用FlagsAttribute,以便枚举可以容纳多个值。 I can then make a field private const Browsers = Browsers.Firefox | Browsers.Chrome 然后,我可以将字段private const Browsers = Browsers.Firefox | Browsers.Chrome private const Browsers = Browsers.Firefox | Browsers.Chrome and I'm good. private const Browsers = Browsers.Firefox | Browsers.Chrome ,我很好。 In this situation. 在这种情况下。

Applying the FlagsAttribute thus allowing for multiple values is not so good in a situation where I need only a single value. 在仅需要一个值的情况下,应用FlagsAttribute从而允许多个值并不是很好。 In those situations I will always have to first check that the enum has only a single value (I do that by checking if it is a power of 2). 在这种情况下,我将始终必须首先检查枚举只有一个值(我通过检查它是否为2的幂来做到这一点)。 I find that a bit annoying. 我觉得有点烦。 Trading maintainability in one situation for less maintainability in other situations - not ideal. 在一种情况下要交换可维护性,而在其他情况下要减少可维护性-不理想。

So my question is is there some smart way to get around this? 所以我的问题是,有什么聪明的方法可以解决这个问题吗? I thought of making two separate enums one with FlagsAttribute and one without however I will then have to maintain all the values of the enums in two places instead of one. 我曾想过用FlagsAttribute创建两个单独的枚举,一个不带FlagsAttribute,但是我将不得不在两个地方而不是一个地方维护所有枚举的值。 Again not ideal. 再次不理想。

Have I missed something that can solve this in a way that does not trade off maintainability anywhere? 我是否错过了可以在任何地方都无法权衡的可维护性的情况下解决此问题的方法?

UPDATE: What I wanted my code to look like is something like this: 更新:我希望我的代码看起来像这样:

public class SomeClass {
  private const Browsers[] browsers = new Browsers[] { Browsers.Firefox, Browsers.Chrome };

  [Attr(browsers)]
  public void SomeMethod() { }

  [Attr(browsers)]
  public void SomeOtherMethod() { }
}

However that results in a compile error. 但是,这会导致编译错误。

An example of a place where I use the enum with a single value only: 我仅将枚举与单个值一起使用的地方的示例:

public void SomeMethod(Browsers browser) {
  /* When using Flags I need to verify that browser contains only a single value */
  switch(browser)
  {
    case Browsers.Firefox:
      /* do something */
      break;
    case Browsers.Chrome:
      /* do something */
      break;
    /* cases for the rest of the values */
  }

}

My question is: can I somehow create two enums with the same values, one of which has the Flags attribute and the other not having it? 我的问题是:我可以以某种方式创建两个具有相同值的枚举,其中一个具有Flags属性,另一个不具有它吗?

Bonus question: is there a smarter way around this that I am just not seeing? 额外的问题:是否有更聪明的方法,我只是没有看到?

Multiple enum members may share the same associated value. 多个枚举成员可以共享相同的关联值。 The example 这个例子

enum Color 
{
   Red,
   Green,
   Blue,
   Max = Blue
}

shows an enum in which two enum members—Blue and Max—have the same associated value. 显示一个枚举,其中两个枚举成员Blue和Max具有相同的关联值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM