1.应用的命名空间
using System.ComponentModel;
using System.Reflection;
2、定义枚举类

1 public enum BagType 2 { 3 [Description("书包1")] 4 schoolbag1, 5 6 [Description("书包2")] 7 schoolbag2, 8 9 [Description("书包3")] 10 schoolbag3 11 }
3、获取枚举类所有属性的描述信息

1 public void GetEnumDesc<Ttype>(Ttype Enumtype) 2 { 3 if (Enumtype == null) throw new ArgumentNullException("Enumtype"); 4 if (!Enumtype.GetType().IsEnum) throw new Exception("参数类型不正确"); 5 6 FieldInfo[] fieldinfo = Enumtype.GetType().GetFields(); 7 foreach (FieldInfo item in fieldinfo) 8 { 9 Object[] obj = item.GetCustomAttributes(typeof(DescriptionAttribute), false); 10 if (obj != null&&obj.Length!=0) 11 { 12 DescriptionAttribute des = (DescriptionAttribute)obj[0]; 13 Console.WriteLine(des.Description); 14 } 15 } 16 }
4、知识总结:
4.1熟悉FieldInfo的用法和属性(发现字段特性并提供对字段员数据的访问权)
4.2理解C#泛型的应用
这个方法如果大家在项目当中扩展的好可以起到一定的举足轻重的作用。如果有什么建议的同仁;希望多多指点;感激不尽;您的指点让我又进步了一点。