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