一个读取C#特性Description方法(zhuan)
class Program { static void Main(string[] args) { string str= DB.write.ToDescription(); Console.WriteLine(str); Console.ReadKey(); } } public enum DB{ [Description("读")] write=1, [Description("写")] read=2 } public static class MyException { /// <summary> /// 获取枚举类型的描述 /// </summary> /// <param name="enumeration"></param> /// <returns></returns> public static string ToDescription(this Enum enumeration) { Type type = enumeration.GetType(); MemberInfo[] memInfo = type.GetMember(enumeration.ToString()); if (null != memInfo && memInfo.Length > 0) { object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false); if (null != attrs && attrs.Length > 0) return ((DescriptionAttribute)attrs[0]).Description; } return enumeration.ToString(); } }