根据索引获取转换枚举值,根据枚举值转换获取索引


public enum color {red=1,green=2,blue=3}

Type typ = typeof(color);

1、根据 索引获取 字符串  结果为 “red”  字符串

string s = typ.GetEnumName(1);  

2、根据字符串获取索引 结果为 0
string s = Enum.Format(typ, Enum.Parse(typ, "Red"), "d");  

3、将枚举字符串如  1、的"red" 转换为  枚举类型

int type=1

color=(color)Enum.Parse(typeof(color), typeof(color).GetEnumName(type));

原文地址:https://www.cnblogs.com/wdw31210/p/2742389.html