C# 特性

给枚举添加特性,通过反射获取特性字符,有些需要把枚举显示在界面选项的地方有用

 public static  class EnumDescription
    {
        /// <summary>
        /// 获取枚举的Description
        /// </summary>
        /// <param name="value">枚举值</param>
        /// <param name="nameInstead">当枚举没有定义description时,是否使用枚举名代替,默认使用</param>
        /// <returns>枚举的description</returns>
        public static string GetDescription(this Enum value, Boolean nameInstead = true)
        {
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            if (name == null)
            {
                return null;
            }
            FieldInfo field = type.GetField(name);
            DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))
                as DescriptionAttribute;
            if (attribute == null && nameInstead == true)
            {
                return name;
            }
            return attribute == null ? null : attribute.Description;

        }
        /// <summary>
        /// 吧枚举转换为键值对集合
        /// </summary>
        /// <param name="enumType">枚举类型</param>
        /// <param name="getText">或者值文本</param>
        /// <returns>以枚举值为key,枚举文本为value的键值对集合</returns>
        public static Dictionary<Int32, String> EnumToDescription(Type enumType, Func<Enum, String> getText)
        {
            if (!enumType.IsEnum)
            {
                throw new ArgumentException("传入的参数必需是枚举类型!", "enumType");

            }
            Dictionary<int ,string>enumDic=new Dictionary<int,string>();
            Array enumValues=Enum.GetValues(enumType);
            foreach(Enum enumValue in enumValues)
            {
                Int32 key=Convert.ToInt32(enumValue);
                String value=getText(enumValue);
                enumDic.Add(key,value);
            }
            return enumDic;

        }
    }

 

第一个方法为枚举扩展方法,如果枚举没有Description 默认输出为枚举名,否则输出空

第二个方法经枚举转换为以枚举值为key,以Description为value的键对集合

实例

     public enum Person
    {
        [Description("张三")]
        zhangsan=1,
        [Description("李四")]
        lisi=2,
        wangwu
    }

 class Program

{

 static void Main(string[] args)

{

            Person p = Person.wangwu;
            Console.WriteLine(p.GetDescription(false));
            Console.WriteLine(p.GetDescription());
            p = Person.zhangsan;
            Console.WriteLine(p.GetDescription());
            Dictionary<Int32, String> dic = EnumDescription.EnumToDescription(typeof(Person),
                e => e.GetDescription());
           
            printdic(dic);

}

      static void printdic(Dictionary<Int32, String> dic)
        {
            foreach (KeyValuePair<Int32, String> item in dic)
            {
                Console.WriteLine("{0}--{1}", item.Key, item.Value);
            }
        }

}

结果:

 

 

Disciption  需要引用using System.ComponentModel;

FieldInfo  需要引用using System.Reflection;

 

关于扩展方法:

扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。

必须是静态类,写法如第一个方法

=> 符号:

lamda表达式····

e=>e.GetDescription()

类似 

string getText(Person p)

{

return p.GetDescription();

}

 

posted @ 2015-08-11 21:12  季末的寂寞  阅读(209)  评论(0编辑  收藏  举报