遍历枚举

 public static List<KeyValuePair<string, string>> Ergodic(Type enumType)
        {
            List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
            if (!enumType.IsEnum) return null;
            foreach (var item in Enum.GetNames(enumType))
            {
                FieldInfo fieldInfo = enumType.GetField(item);
                DescriptionAttribute descriptionAttribute = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute;
                if (descriptionAttribute != null)
                {
                    string iName = Enum.Format(enumType, Enum.Parse(enumType, item), "d");
                    list.Add(new KeyValuePair<string, string>(iName, descriptionAttribute.Description));
                }
            }
            return list;
        }

 

posted @ 2016-04-26 11:13  左立军  阅读(278)  评论(0编辑  收藏  举报