如何创建枚举数组
我有大约 30 个不同标记的枚举,我想将它们放入一个数组中以进行索引和快速访问。我还要澄清一下,我没有 1 个具有 30 个值的枚举,但我有 30 个具有不同数量值的枚举。
目标是将它们添加到数组的指定索引处。这样我就可以编写一个函数,在其中可以将数组索引传递到其中以设置枚举的微粒值。
更新: 这是我想做的一个例子。
枚举主( 枚举1 = 0, 枚举2 = 1, 的索引相匹配的索引
enumn = n-1 ) - 这具有与关联枚举[flag] enum1(值1=0, 值2=1, 值3=2, 值4=4...)
[标志] enum2("")
[标志] 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
}
I have about 30 different flagged enums that I would like to put into an array for indexing and quick access. Let me also claify that I do not have 1 enum with 30 values but I have 30 enums with differing amounts of values.
The goal would be to add them to an array at a specifed index. This way I can write a functions in which I can pass the array index into for setting particuler values of the enum.
Updated:
Here is an example of what I am wanting to do.
enum main(
enum1 = 0,
enum2 = 1,
enumn = n-1 ) - this has indexs which would match the index of the associated enum
[flag]
enum1(value1=0, value2=1, value3=2, value4=4...)
[flag]
enum2("")
[flag]
enum2("")
since I am using flagable enums I have a class like the following
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您有 30 种不同类型的枚举,因此您无法为它们创建强类型数组。最好的方法是使用 System.Enum 数组:
如果需要强类型枚举值,则在从数组中提取枚举时必须进行强制转换。
Since you have 30 different types of enums, you can't create a strongly typed array for them. The best you could do would be an array of System.Enum:
You would then have to cast when pulling an enum out of the array if you need the strongly typed enum value.
如果我理解正确,你必须这样做:
但是你必须像这样访问(不是强类型,并且很容易引用错误的索引):
在.NET 4中,你可以使用元组:
...但是我不会推荐它用于这么多(30)个枚举,我认为甚至有8个左右的限制。您还可以创建自己的类:
我建议使用最后一种方法,因为您使用的是引用类型,不依赖于索引位置,您可以为属性提供任何您想要的名称,并且它是强类型的。您唯一“失去”的是轻松迭代列表的能力,但如果您确实需要,可以使用反射来实现这一点。
If I understand correctly, you would have to do:
But then you would have to access like this (not strongly typed, and easy to reference the wrong index):
In .NET 4, you could use the Tuple:
...but I wouldn't recommend it for so many (30) enums, and I think there's even a limit of 8 or so. You can also create your own class:
I would recommend the last way because you're using a reference type, you're not dependent on index position, you can give the properties whatever name you want, and it's strongly typed. The only thing you "lose" is the ability to easily iterate through the list, but you can achieve that with Reflection if you really need to.
您始终可以使用旧的
object[]
,但这意味着需要进行大量转换。You could always use good old
object[]
, but that means doing a lot of casting.Enum 提供了两种将整数转换为枚举值的机制 - GetValues() 方法和普通转换:
如果您可以使用这些机制之一从国际。
Enum provides two mechanisms for converting an integer to an enum value - the GetValues() method and plain casting:
I'm not quite sure why you want to create your own arrays if you can use one of these mechanisms to get an enum from an int.