枚举类型转换成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; }