码迷,mamicode.com
首页 > 其他好文 > 详细

枚举类型转换成Dictionary

时间:2014-08-06 14:11:01      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   os   io   for   ar   

定义的枚举

 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;
        }

 

枚举类型转换成Dictionary,布布扣,bubuko.com

枚举类型转换成Dictionary

标签:des   style   blog   color   os   io   for   ar   

原文地址:http://www.cnblogs.com/FH-cnblogs/p/3894137.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!