C # enumeration, passing int value returns a string value

Source: Internet
Author: User

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

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.