Enum extended feature example code

零下一度
Release: 2017-06-24 10:35:45
Original
1625 people have browsed it

Due to the inherent flaw in English, I always use Chinese when enumerating, so that I don’t need to read the comments to know the meaning of the enumeration. Today I saw the blog post

Using attributes instead of directly using Chinese as attributes. The part is specially excerpted for future use

    /// <summary>/// 枚举帮助类/// </summary>public static class EnumTools
    {/// <summary>///  获取当前枚举值的描述和排序/// </summary>/// <param name="value"></param>/// <returns>返回元组Tuple(string,int)</returns>public static Tuple<string,int> GetDescription(this Enum value)
        {int order = 0;string description = string.Empty;

            Type type = value.GetType();// 获取枚举FieldInfo fieldInfo = type.GetField(value.ToString());// 获取枚举自定义的特性DescriptionAttributeobject[] attrs = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            DescriptionAttribute attr = (DescriptionAttribute)attrs.FirstOrDefault(a => a is DescriptionAttribute);

            description = fieldInfo.Name;if (attr != null)
            {
                order = attr.Order;
                description = attr.Name;
            }return new Tuple<string,int>(description,order);

        }/// <summary>/// 获取当前枚举的所有描述/// </summary>/// <returns></returns>public static List<KeyValuePair<int, string>> GetAll<T>()
        {return GetAll(typeof(T));
        }/// <summary>/// 获取所有的枚举描述和值/// </summary>/// <param name="type"></param>/// <returns></returns>public static List<KeyValuePair<int, string>> GetAll(Type type)
        {

            List<EnumToolsModel> list = new List<EnumToolsModel>();// 循环枚举获取所有的Fieldsforeach (var field in type.GetFields())
            {// 如果是枚举类型if (field.FieldType.IsEnum)
                {object tmp = field.GetValue(null);
                    Enum enumValue = (Enum)tmp;int intValue = Convert.ToInt32(enumValue);var dec = enumValue.GetDescription();int order= dec.Item2;string showName = dec.Item1; // 获取描述和排序list.Add(new EnumToolsModel { Key = intValue, Name = showName, Order = order });
                }
            }// 排序并转成KeyValue返回return list.OrderBy(i => i.Order).Select(i => new KeyValuePair<int, string>(i.Key, i.Name)).ToList();

        }/// <summary>/// 枚举Model/// </summary> partial class EnumToolsModel
        {public int Order { get; set; }public string Name { get; set; }public int Key { get; set; }
        }

    }/// <summary>/// 枚举特性/// </summary>[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]public class DescriptionAttribute : Attribute
    {/// <summary>/// 排序/// </summary>public int Order { get; set; }/// <summary>/// 名称/// </summary>public string Name { get; set; }/// <summary>/// 定义描述名称/// </summary>/// <param name="name">名称</param>public DescriptionAttribute(string name)
        {
            Name = name;
        }/// <summary>/// 定义描述名称和排序/// </summary>/// <param name="name">名称</param>/// <param name="order">排序</param>public DescriptionAttribute(string name, int order)
        {
            Name = name;
            Order = order;
        }

    }
Copy after login
View Code

Replace the out parameter in the original text with the return element Group, since the project is developed in vs2015, it cannot use c#7.0 features, otherwise it would be better to use the value tuple in 7.0. Performance and display friendliness will be improved.

The above is the detailed content of Enum extended feature example code. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!