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

 

 

 

 

 

posted @ 2017-05-04 15:44  Doc.stu  阅读(285)  评论(0编辑  收藏  举报