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.