好吧,我不认为标题说得对......但是这里有:
我有一个大约有40个枚举的课程.即:
Class Hoohoo
{
public enum aaa : short
{
a = 0,
b = 3
}
public enum bbb : short
{
a = 0,
b = 3
}
public enum ccc : short
{
a = 0,
b = 3
}
}
Run Code Online (Sandbox Code Playgroud)
现在说我有一个字符串和值的字典,每个字符串是上面提到的枚举的名称:
Dictionary<string,short>{"aaa":0,"bbb":3,"ccc":0}
Run Code Online (Sandbox Code Playgroud)
我需要将"aaa"更改为HooBoo.aaa以查找0.由于枚举是静态的,因此似乎无法找到方法.否则,我将不得不为每个枚举编写一个方法来将字符串绑定到它.我可以这样做,但那就是编写的代码.
谢谢,Cooter
您必须使用反射来获取底层枚举类型:
Type t = typeof(Hoohoo);
Type enumType = t.GetNestedType("aaa");
string enumName = Enum.GetName(enumType, 0);
Run Code Online (Sandbox Code Playgroud)
如果你想获取实际的枚举值,你可以使用:
var enumValue = Enum.Parse(enumName, enumType);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1168 次 |
| 最近记录: |