public enum Color
{
[Description("蓝色")]
Blue = 1,
[Description("红色")]
Red = 2,
[Description("黑色")]
Black = 3
}
1.获取枚举注释
public static string GetEnumDesc(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false);
return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
}
2.枚举转字符串
string enumStringOne = color.ToString(); //效率低,不推荐
string enumStringTwo = Enum.GetName( typeof (Color), color);//推荐
3.字符串转枚举
Color enumOne = (Color)Enum.Parse( typeof (Color), colorString);
4.枚举转值
int enumValueOne = color.GetHashCode();
int enumValueTwo = ( int )color;
int enumValueThree = Convert.ToInt32(color);
5.字符串转值
int enumValue = ( int )Enum.Parse( typeof (Color), colorString);
6.值转枚举
Color enumOne = (Color)colorValue;
Color enumTwo = (Color)Enum.ToObject( typeof (Color), colorValue);
7.值转字符串
string enumString = Enum.GetName( typeof (Color), colorValue);