• 【C#】枚举


    枚举

    public static class CommonEnums
        {
            public enum people
            {
                /// <summary>
                ///男人
                /// </summary>
                [Description("男人")]
                man = 1,
                /// <summary>
                /// 女人
                /// </summary>
                [Description("女人")]
                women = 2,
            }
    }

    string->枚举

    CommonEnums.people en = (CommonEnums.people)Enum.Parse(typeof(CommonEnums.people), "women");

    枚举->Dictionary

    //使用:生成一个dictionary
    Dictionary<int, string> dic=CommonEnums.people.man.ToEnumDictionary();
    
    //静态方法
    public static class EnumExtension
        {
            public static Dictionary<int, string> ToEnumDictionary(this Enum enumT, string category = "")
            {
                Type enumType = enumT.GetType();
                if (!enumType.IsEnum) throw new Exception("参数不是枚举");
    
                var array = enumType.GetEnumValues();
                if (array.Length < 1) return new Dictionary<int, string>(1);
                Dictionary<int, string> result = new Dictionary<int, string>(array.Length);
    
                int[] enumValues = new int[array.Length];
                int index = 0;
    
                foreach (var item in array)
                {
                    enumValues[index] = Convert.ToInt32(item);
                    ++index;
                }
    
                var enumNames = enumType.GetEnumNames();
                DescriptionAttribute[] descriptions = null;
                CategoryAttribute[] categorys = null;
    
                for (index = 0; index < array.Length; ++index)
                {
                    FieldInfo fi = enumType.GetField(enumNames[index]);
                    descriptions = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
    
                    if (category != string.Empty)
                    {
                        categorys = fi.GetCustomAttributes(typeof(CategoryAttribute), false) as CategoryAttribute[];
                        if (categorys != null && categorys.Length > 0)
                        {
                            if (categorys[0].Category != category)
                            {
                                continue;
                            }
                        }
                    }
    
                    if (descriptions != null && descriptions.Length > 0)
                    {
                        result.Add(enumValues[index], descriptions[0].Description);
                    }
                    else
                    {
                        result.Add(enumValues[index], string.Empty);
                    }
                }
    
                return result;
            }
        }

    获取枚举描述

    //使用
    string desc = CommonEnums.people.man.GetDescription();
    //方法
    public static string GetDescription(this Enum value, bool nameInstend = 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 && nameInstend == true)
                {
                    return name;
                }
                return attribute == null ? null : attribute.Description;
            }

     

  • 相关阅读:
    cmd git常用命令使用
    vue2.0 vue-qr生成二维码
    全局css设置
    input 输入框 json格式展示
    eslint 常用配置及说明
    es6中promise 使用总结
    vue 自定义全局组件
    vue element 表头添加斜线
    indexedDB 使用
    黑客攻防从入门到精通(攻防与脚本编程篇)
  • 原文地址:https://www.cnblogs.com/stgp/p/6807507.html
Copyright © 2020-2023  润新知