Enumeration of Enum in C # Some of the methods of conversion are organized

Source: Internet
Author: User

Some conversions of enumerations are often encountered in the work, especially to get enumeration notes, etc. specially collated methods later used

public void TestMethod1 () {Testenumone colorenum = testenumone.red;            int colorvalue = 0x0000FF;            String colorstr = "Red";            String colordes = "Red";            Enumeration to enumeration String colorstr = Colorenum.tostring ();            Colorstr = Enum.getname (typeof (Testenumone), colorenum);            Enumeration value to enumeration string colorstr = Enum.getname (typeof (Testenumone), colorvalue);            Enumeration to enumeration value ColorValue = Colorenum.gethashcode ();            ColorValue = (int) colorenum; Enumeration character to enumeration value ColorValue = Enum.parse (typeof (Testenumone), colorstr).            GetHashCode ();            ColorValue = (int) enum.parse (typeof (Testenumone), colorstr);            Enumeration Word to enumeration Colorenum = (Testenumone) enum.parse (typeof (Testenumone), colorstr);            enumeration value to enum Colorenum = (testenumone) colorvalue;            Get comments based on enumerations Colordes = TestEnumOne.Red.GetEnumDescriptionByEnum (typeof (Testenumone)); RootThe enumeration value gets the comment colordes = TestEnumOne.Blue.GetEnumDescriptionByEnumValue (typeof (Testenumone), colorvalue);        Get comments based on enumeration string colordes = TestEnumOne.Blue.GetEnumDescriptionByEnumString (typeof (Testenumone), colorstr); }

Here is an enumeration and some of the methods used above

 Public Static classEnumclass { Public enumTestenumone {[Description ("Red")] Red=0xff0000, [Description ("Orange")] Orange=0xffa500, [Description ("Yellow")] Yellow=0xffff00, [Description ("Blue")] Blue=0x0000FF,        }        /// <summary>        ///get comments based on enumerations/// </summary>        /// <param name= "Aenum" >Enumeration</param>        /// <param name= "Enumtype" >Enum Type</param>        /// <returns>Notes</returns>         Public Static stringGetenumdescriptionbyenum ( ThisEnum Aenum, System.Type enumtype) {            stringEnumdescription =string.            Empty; foreach(System.Enum EnumiteminchSystem.Enum.GetValues (enumtype)) {                stringEnumstring =enum.getname (Enumtype, aenum); if(Enumstring.tolower () = =enum.getname (Enumtype, Enumitem). ToLower ()) {FieldInfo fi=Enumtype.getfield (enumstring); DescriptionAttribute da=(DescriptionAttribute) Attribute.GetCustomAttribute (FI, enumtype); if(Da! =NULL) {enumdescription=da.                    Description; }                }            }            returnenumdescription; }        /// <summary>        ///get comments based on enumeration values/// </summary>        /// <param name= "Aenum" >Enumeration</param>        /// <param name= "Enumtype" >Enum Type</param>        /// <param name= "Enumvalue" >enumeration Values</param>        /// <returns>Notes</returns>         Public Static stringGetenumdescriptionbyenumvalue ( ThisEnum Aenum, System.Type enumtype,intenumvalue) {            stringEnumdescription =string.            Empty; foreach(System.Enum EnumiteminchSystem.Enum.GetValues (enumtype)) {                if(Enumitem.gethashcode () = = (int) {enumvalue) {stringEnumstring =enum.getname (Enumtype, enumvalue); FieldInfo fi=Enumtype.getfield (enumstring); DescriptionAttribute da=(DescriptionAttribute) Attribute.GetCustomAttribute (FI, enumtype); if(Da! =NULL) {enumdescription=da.                    Description; }                }            }            returnenumdescription; }        /// <summary>        ///get comments based on an enumeration string/// </summary>        /// <param name= "Aenum" >Enumeration</param>        /// <param name= "Enumtype" >Enum Type</param>        /// <param name= "Enumvalue" >enumeration Values</param>        /// <returns>Notes</returns>         Public Static stringGetenumdescriptionbyenumstring ( ThisEnum Aenum, System.Type enumtype,stringenumstring) {            stringEnumdescription =string.            Empty; foreach(System.Enum EnumiteminchSystem.Enum.GetValues (enumtype)) {                if(Enumstring.tolower () = =enum.getname (Enumtype, Enumitem). ToLower ()) {FieldInfo fi=Enumtype.getfield (enumstring); DescriptionAttribute da=(DescriptionAttribute) Attribute.GetCustomAttribute (FI, enumtype); if(Da! =NULL) {enumdescription=da.                    Description; }                }            }            returnenumdescription; }    }

Enumeration of Enum in C # Some of the methods of conversion are organized

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.