About the enumeration types in C # support extension instructions for displaying Chinese _c# tutorial

Source: Internet
Author: User

Copy Code code as follows:

AuditEnum.cs:

public enum Auditenum
{
Holding=0,
  
Auditing=1,

pass=2,

Reject=3
}

In ASP.net, for example, a method in a program might use an enumeration value like this:
public void Handleaudit (int userID, Auditenum ae)
{
if (Ae==auditenum.pass)
{
Do something
}
else if (ae==auditenum.reject)
{
Do other Something
}
}

asp.net pages often need to display Chinese enumeration information:


Serial number
Project
State
Auditor person

Leave list
Audit through
Tom


Workaround: Add DescriptionAttribute to the enumerator and use reflection to get the Chinese information.

Steps:

1. Add the namespace System.ComponentModel to the class that defines the enumeration auditenum, and add DescriptionAttribute to each of the enumerated items, as shown in the sample code:

Copy Code code as follows:

Using System.ComponentModel;

public enum Auditenum
{
[Description ("Not submitted for trial")]
Holding=0,

[Description (in audit)]
Auditing=1,

[Description ("Audit passed")]
pass=2,

[Description ("dismiss")]
Reject=3
}


2. Customizing a class EnumService.cs, adding static methods GetDescription () reads description information based on the enumerated values passed in, with the sample code as follows:

Copy Code code as follows:

public class Enumservice
{
public static string getdescription (Enum obj)
{
string objname = obj. ToString ();
Type t = obj. GetType ();
FieldInfo fi = T.getfield (objname);

Descriptionattribute[] Arrdesc = (descriptionattribute[]) fi. GetCustomAttributes (typeof (DescriptionAttribute), false);
    
Return arrdesc[0]. Description;
}
}


3. Increase the call to Enumservice.getdescription () at the point where the enumeration value is output, as shown in the sample code:

Copy Code code as follows:

asp.net page code:
<asp:repeater id= "Auditrepeater" runat= "Server" onitemdatabound= "Auditrepeater_onitemdatabound" >
<ItemTemplate>
Something UI code is here ....
<asp:literal id= "Audittext" runat= "Server" ></asp:Literal>
Something UI code is here ....
</ItemTemplate>
</asp:Repeater>


asp.net page background code:
protected void Auditrepeater_onitemdatabound (object sender, RepeaterItemEventArgs arg)
{
if (Arg. Item.itemtype = = ListItemType.Item)
{
Literal audit = Arg. Item.findcontrol ("Audittext") as Literal;

Auditenum ae = auditenum.pass; Assign a value based on the actual situation of the project, here for the simplified assignment auditenum.pass
Audit. Text = Enumservice.getdescription (We);
}
}


Complete the full text.

The above code runs in VS2010, has any question please leave a message below, likes on the point recommendation.

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.