【发布时间】:2011-06-15 06:51:16
【问题描述】:
我认为这个问题需要一些代码:
private TypeValues GetEnumValues(Type enumType, string description)
{
TypeValues wtv = new TypeValues();
wtv.TypeValueDescription = description;
List<string> values = Enum.GetNames(enumType).ToList();
foreach (string v in values)
{
//how to get the integer value of the enum value 'v' ?????
wtv.TypeValues.Add(new TypeValue() { Code = v, Description = v });
}
return wtv;
}
这样称呼它:
GetEnumValues(typeof(AanhefType), "some name");
在GetEnumValues 函数中,我有枚举的值。所以我迭代了这些值,我也想得到那个枚举值的整数值。
所以我的值是“红色”和“绿色”,我也想得到 0 和 1。
当我的函数中有枚举时,我可以从字符串创建枚举值并将其转换为该枚举,然后将其转换为 int,但在这种情况下,我没有枚举本身,但是只有枚举的类型。
我也尝试将实际枚举作为参数传递,但我不允许将枚举作为参数传递。
所以现在我被困住了.....
【问题讨论】: