一个读取C#特性Description方法(zhuan)

2021-09-22 16:14

阅读:918

标签:getc   tomat   als   str   获取   false   name   特性   custom    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 { /// /// 获取枚举类型的描述 /// /// /// 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(); } } View Code  一个读取C#特性Description方法(zhuan)标签:getc   tomat   als   str   获取   false   name   特性   custom   原文地址:http://www.cnblogs.com/mamiyiya777/p/7358120.html


评论


亲,登录后才可以留言!