How can I get an enum name from the name of the enum type and an int
This question already has an answer here:
In addition to the name of enum itself you should know namespace and assembly that enum is declared at. Assuming you run code below in assembly where enum is defined and all your enums are defined in the same namespace you know at compile time, you can do it like that:
string dataType = "Cheese";
int dataValue = 3;
var enumType = Type.GetType("Namespaces.Of.Your.Enums." + dataType);
var name = Enum.GetName(enumType, dataValue);
If your enum is in another assembly than this code is running at - you will need to provide assembly qualified name of your enum type (again with namespace).
Type t = Type.GetType(dataType)
string value = Enum.GetName(t, dataValue)
应该做的伎俩(未编译,在地铁上)
enum.Parse() would be your friend here.
Like so:
int cheeseType = 1;
(Cheese)Enum.Parse(typeof(Cheese), cheeseType.ToString());
链接地址: http://www.djcxy.com/p/22524.html
上一篇: 将方法参数转换为枚举