zoukankan      html  css  js  c++  java
  • 给枚举定义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 = getText(enumValue);
                    enumDic.Add(key, value);
                }
                return enumDic;
            }
  • 相关阅读:
    我不知道 大家 现在 为什么 还那么费力 的 去 学习 群论
    ( 1 / x ) ^ x , x -> 无穷 的 极限 是 什么 ?
    从 庞加莱猜想 说起
    《求助吧友数学分析》 里 的 题目
    手扶拖拉机 同学 的 一些 极限题
    在 《K哥大师,我感觉那道题弄不出来》 里 的 回复
    在 《数学问题,最佳曲面求解实例》 里 的 回复
    杨辉三角开方公式 和 n次方和公式
    2016年3月31号起 随着自己的学习,我将把自己的笔记整理到博客园
    Java数据库编程、XML解析技术
  • 原文地址:https://www.cnblogs.com/sylone/p/6229354.html
Copyright © 2011-2022 走看看