标签:des style blog color os io for ar
public Enum demo
{
[Description("我")]
wo=0;
[Description("你")]
ni=1;
[Description("他")]
ta=2;
}
通过中间获得枚举的值,0、1、2
1 public static class EnumHelper 2 { 3 public static string GetDescription<T>(this T value) where T : IFormattable, IConvertible, IComparable 4 { 5 FieldInfo fi = value.GetType().GetField(value.ToString()); 6 DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); 7 return (attributes.Length > 0) ? attributes[0].Description : value.ToString(); 8 } 9 }
调用GetDescription()泛型方法,得到你要显示的字符串
枚举值按视图效果要求显示的处理方法,布布扣,bubuko.com
标签:des style blog color os io for ar
原文地址:http://www.cnblogs.com/zychengzhiit1/p/3919474.html