The Enum Extension Feature replaces the Chinese attribute and the enum extension feature.
Due to a natural defect in English, I have been using Chinese during enumeration so that I can understand the meaning of enumeration without reading comments.
Bytes. Specially excerpted parts for future use convenience
/// <Summary> /// enumeration help class /// </summary> public static class EnumTools {/// <summary> /// obtain the description and sort /// </summary> /// <param name = "value"> </param> /// <returns> returns the Tuple (string, int) </returns> public static Tuple <string, int> GetDescription (this Enum value) {int order = 0; string description = string. empty; Type type = value. getType (); // obtain the enumerated FieldInfo fieldInfo = type. getField (value. toString () ); // Obtain the DescriptionAttribute object [] 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> /// obtain all the descriptions of the current enumeration /// </summary> /// <returns> </returns> public static List <KeyValuePair <int, string >>> GetAll <T> () {return GetAll (typeof (T ));} /// <summary> /// obtain all enumeration descriptions and values /// </summary> /// <param name = "type"> </param> /// <returns> </returns> public static List <KeyValuePair <int, string >>> GetAll (Type type) {List <EnumToolsModel> list = new List <EnumToolsModel> (); // obtain all Fields foreach (var field in type. getFields () {// if it is an enumeration type 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; // obtain the description and sort list. add (new EnumToolsModel {Key = intValue, Name = showName, Order = order}) ;}// sort and convert to KeyValue to return list. orderBy (I => I. order ). select (I => new KeyValuePair <int, string> (I. key, I. name )). toList () ;}//< summary> /// enumeration Model /// </summary> partial class EnumToolsModel {public int Order {get; set ;} public string Name {get; set;} public int Key {get; set ;}}} /// <summary> /// enumeration features /// </summary> [AttributeUsage (AttributeTargets. field, AllowMultiple = false, Inherited = false)] public class DescriptionAttribute: Attribute {// <summary> /// sort /// </summary> public int Order {get; set ;}//< summary> /// Name /// </summary> public string Name {get; set ;} /// <summary> /// define the description name /// </summary> /// <param name = "name"> name </param> public DescriptionAttribute (string name) {Name = name ;} /// <summary> /// define the description name and sorting /// </summary> /// <param name = "name"> name </param> /// <param name = "order"> sort </param> public DescriptionAttribute (string name, int order) {Name = name; Order = order ;}}
View Code
Replace the out parameter in the original text with the returned tuples. Because the project is developed in vs2015, the c #7.0 feature cannot be used. Otherwise, it is better to use the value tuples in 7.0. Both Performance and display friendliness are improved.