首页 > 代码库 > C# 给枚举定义DescriptionAttribute,把枚举转换为键值对

C# 给枚举定义DescriptionAttribute,把枚举转换为键值对

在C#中,枚举用来定状态值很方便,例如我定义一个叫做Season的枚举

public enum Season    {        Spring = 1,        Summer = 2,        Autumn = 3,        Winter = 4    }

枚举名是不能出现空格,()-/等字符

我们想把Spring显示为春天,我们要自己定义说明信息,我们可以使用DescriptionAttribute,如下

public enum Season    {        [Description("春 天")]        Spring = 1,        [Description("夏 天")]        Summer = 2,        //[Description("秋 天")]        Autumn = 3,        [Description("冬 天")]        Winter = 4    }

下面我们来写个扩展方法,来得到枚举的说明信息,如下

        /// <summary>        /// 扩展方法,获得枚举的Description        /// </summary>        /// <param name="value">枚举值</param>        /// <param name="nameInstead">当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认是使用</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> EnumToDictionary(Type enumType, Func<Enum, String> getText)        {            if (!enumType.IsEnum)            {                throw new ArgumentException("传入的参数必须是枚举类型!", "enumType");            }            Dictionary<Int32, String> enumDic = new Dictionary<int, string>();            Array enumValues = Enum.GetValues(enumType);            foreach (Enum enumValue in enumValues)            {                Int32 key = Convert.ToInt32(enumValue);                String value = http://www.mamicode.com/getText(enumValue);>

我们把上面两个方法放入名为EnumUtil的静态类中。

下面是使用方法

public enum Season    {        [Description("春 天")]        Spring = 1,        [Description("夏 天")]        Summer = 2,        //[Description("秋 天")]        Autumn = 3,        [Description("冬 天")]        Winter = 4    }    class Program    {        static void Main(string[] args)        {            Season spring = Season.Spring;            //打印枚举名            Console.WriteLine(spring.ToString());            //打印枚举说明            Console.WriteLine(spring.GetDescription());            //枚举转换为键值对集合            Dictionary<Int32, String> dic = EnumUtil.EnumToDictionary(typeof(Season), e => e.GetDescription());            PrintDic(dic);            dic = EnumUtil.EnumToDictionary(typeof(Season), e => e.GetDescription(false));            PrintDic(dic);            dic = EnumUtil.EnumToDictionary(typeof(Season), e => e.ToString());            PrintDic(dic);            dic = EnumUtil.EnumToDictionary(typeof(Season), e => Enum.GetName(typeof(Season), e));            PrintDic(dic);            Console.ReadLine();        }        private static void PrintDic(Dictionary<Int32, String> dic)        {            foreach (KeyValuePair<Int32,String> item in dic)            {                Console.WriteLine("Key:{0}-----Value:{1}", item.Key, item.Value);            }        }    }

输出结果如下


Spring
春 天
Key:1-----Value:春 天
Key:2-----Value:夏 天
Key:3-----Value:Autumn
Key:4-----Value:冬 天
Key:1-----Value:春 天
Key:2-----Value:夏 天
Key:3-----Value:
Key:4-----Value:冬 天
Key:1-----Value:Spring
Key:2-----Value:Summer
Key:3-----Value:Autumn
Key:4-----Value:Winter
Key:1-----Value:Spring
Key:2-----Value:Summer
Key:3-----Value:Autumn
Key:4-----Value:Winter

 

源代码:http://files.cnblogs.com/jm6041/EnumUtil.rar

C# 给枚举定义DescriptionAttribute,把枚举转换为键值对