Demand: 1: The head of the subsidiary 2: Personnel 3: Approver 4: Check Batch person 5: The administrator passed in a value of one of the numbers 1,2,3,4,5. Need to return their Chinese description.
Forget how to write ... Baidu later found out. Record it as a gadget.
Paste the following source code:
//the method required Public stringgetenumdescription (Enum enumvalue) {stringstr =enumvalue.tostring (); System.Reflection.FieldInfo Field=Enumvalue.gettype (). GetField (str); Object[] objs = field. GetCustomAttributes (typeof(System.ComponentModel.DescriptionAttribute),false); if(Objs = =NULL|| Objs. Length = =0)returnstr; System.ComponentModel.DescriptionAttribute da= (System.ComponentModel.DescriptionAttribute) objs[0]; returnda. Description; }
//Defining Enumerations enumRoletype {[Description ("Head of subsidiary")] Zmsmanager=1, [Description ("Group Manpower")] Jthr=2, [Description ("Assessment Person")] Assessperson=3, [Description ("Establishment of persons")] Makeperson=4, [Description ("system Administrator")] Sysmanager=5 }
// Calling Methods string returnvalue = Getenumdescription ((roletype) (Enum.parse (typeof(roletype),"1 "))); Return value string: Head of subsidiary
Reference Blog: http://www.cnblogs.com/xiaofengfeng/p/4125003.html
C # enumeration, passing int value returns a string value