Enum Extension Attribute Instance code

Source: Internet
Author: User
Because of the innate flaws in English, I always use Chinese when enumerating, so I don't need to read the comments to know the enumeration meaning, see the blog today

Use attributes instead of using Chinese directly as attributes. Deliberately excerpt part for later use convenient

    <summary>///Enumeration Helper class///</summary>public static class Enumtools {//<summary>///Gets the description and row of the current enumeration value Order///</summary>///<param name= "value" ></param>///<returns> return tuple tuple (string,int) </ Returns>public static tuple<string,int> getdescription (this Enum value) {int order = 0;string description = String.            Empty; Type type = value. GetType ();//Gets the enumeration fieldInfo FieldInfo = type. GetField (value. ToString ());//Get enumeration custom attributes 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>///Get all descriptions of the current enumeration///</summary>///<returns></returns>public static List<keyvaluepair<int, string>> getall<t> () {return GetAll (typeof (T        )); }///<summary>///Get all enumeration description and values///</summary>///<param name= "type" ></param>///<returns ></returns>public static List<keyvaluepair<int, string>> GetAll (type type) {LIST&L T enumtoolsmodel> list = new list<enumtoolsmodel> ();//Loop enumeration gets all the Fieldsforeach (var field in type. GetFields ()) {//If it is an enum-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; Gets the description and sort list.                ADD (new Enumtoolsmodel {Key = intvalue, Name = showname, order = order}); }}//sort and turn keyvalue back to return list. (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 attribute///</summary>[attributeusage (Attributetargets.field, AllowMultiple = False, Inher ited = False)]public class Descriptionattribute:attribute {//<summary>///sort//</summary>public int Or der {get; set;} <summary>///name///</summary>public string name {get; set;} <summary>///Definition Description Name///</summary>///<param name= "name" > Name </param>public        DescriptionAttribute (string name) {name = name; }///<summary>///Definition description Name and sort///</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 return tuple, because the project is VS2015 development, cannot use the c#7.0 attribute, otherwise the value tuple in 7.0 should be a little better. Performance and display friendliness can be improved.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.