zoukankan      html  css  js  c++  java
  • 枚举类型转换成Dictionary

    定义的枚举

     public enum BudgetShopType : int
        {
            /// <summary>
            /// 装修费预算
            /// </summary>
            [EnumMember(Value = "装修费预算")]
            BudgetDS = 1,
    
            /// <summary>
            /// 设计费预算
            /// </summary>
            [EnumMember(Value = "设计费预算")]
            BudgetDD = 2,
        }

    调用

    BudgetShopType bst = new BudgetShopType();
    ConvertEnumToList(bst.GetType());

    方法

    public static Dictionary<string,string> ConvertEnumToList(Type enumType)
            {
                if (enumType.IsEnum == false) { return null; }
                Dictionary<string,string> list = new Dictionary<string,string>();
                Type typeDescription = typeof(EnumMemberAttribute);
                FieldInfo[] fields = enumType.GetFields();
                string strText = string.Empty;
                string strValue = string.Empty;
                foreach (FieldInfo field in fields)
                {
                    if (field.IsSpecialName) continue;
                    strValue = field.GetRawConstantValue().ToString();
                    object[] arr = field.GetCustomAttributes(typeDescription, true);
                    if (arr.Length > 0)
                        strText = (arr[0] as EnumMemberAttribute).Value;
                    else
                        strText = field.Name;
                    list.Add(strValue,strText);
                }
                return list;
            }
  • 相关阅读:
    持久化类的三种状态
    Hibernate持久化类规则
    JSP之Bean
    JSP动作标签
    JSP九大内置对象
    Jsp指令
    JSTL标签语言
    JSP之EL表达式
    Java 中的 Characters
    汇编基本语法
  • 原文地址:https://www.cnblogs.com/FH-cnblogs/p/3894137.html
Copyright © 2011-2022 走看看