zoukankan      html  css  js  c++  java
  • 【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;
            }

     

  • 相关阅读:
    时序裕量计算之三:两种计算方法的比较
    时序裕量计算之二:Altera计算时序裕量的方法
    关于JTAG信号的约束
    时序裕量计算之五:FPGA输出数据到外部的时序裕量计算
    关于set_clock_groups
    C#与Flash交互
    javascript 代码加密解密代码
    c#远程取得指定url的网页内容
    不可多得的Javascript(AJAX)开发工具 - Aptana
    htmlparser使用指南
  • 原文地址:https://www.cnblogs.com/stgp/p/6807507.html
Copyright © 2011-2022 走看看