如何创建枚举数组

use*_*347 9 c# arrays enums

我有大约30个不同的标记枚举,我想将其放入数组中以进行索引和快速访问.我还要声明,我没有1个包含30个值的枚举,但我有30个具有不同数值的枚举.

目标是将它们添加到指定索引处的数组中.这样我就可以编写一个函数,我可以在其中传递数组索引来设置枚举的粒度值.

更新:这是我想要做的一个例子.

枚举main(enum1 = 0,enum2 = 1,enumn = n-1) - 这个索引与相关枚举的索引相匹配

[flag] enum1(value1 = 0,value2 = 1,value3 = 2,value4 = 4 ......)

[flag] enum2("")

[flag] enum2("")

因为我使用的是可标记的枚举,所以我有一个如下的类

public static class CEnumWorker
{
   public static enum1 myEnum1 = enum1.value1;
   public static enum2 myEnum2 = enum2.value1;
   public static enumN myEnumN = enumN.value1;

   //I would then have functions that set the flags on the enums. I would like to access the enums through an array or other method so that I do not have to build a large switch statement to know which enum I am wanting to manipulate
}
Run Code Online (Sandbox Code Playgroud)

Wes*_*ser 19

由于您有30种不同类型的枚举,因此无法为它们创建强类型数组.你可以做的最好的是一个System.Enum数组:

Enum[] enums = new Enum[] { enum1.Value1, enum2.Value2, etc };
Run Code Online (Sandbox Code Playgroud)

如果你需要强类型的枚举值,那么在从数组中拉出枚举时你必须进行转换.