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);
            }
        }

}

结果:

 图片 1

 

Disciption  必要引用using System.ComponentModel;

FieldInfo  需求引用using System.Reflection;

 

关于扩张方法:

推而广之方法使你能够向现有项目“添加”方法,而无需创设新的派生类型、重新编译或以别的方法修改原始类型。

非得是静态类,写法如首先个点子

=> 符号:

lamda表达式····

e=>e.GetDescription()

类似 

string getText(Person p)

{

return p.GetDescription();

}

 

相关文章