Copy codeThe Code is as follows: AuditEnum. cs:
Public enum AuditEnum
{
Holding = 0,
Auditing = 1,
Pass = 2,
Reject = 3
}
Taking asp.net as an example, an enumeration value may be used in a method in the program as follows:
Public void HandleAudit (int userID, AuditEnum AE)
{
If (AE = AuditEnum. Pass)
{
// Do something
}
Else if (AE = AuditEnum. Reject)
{
// Do other something
}
}
The asp.net page usually needs to display Chinese enumeration information:
Serial number
Project
Status
Reviewer
Application Form
Approved
Zhang San
Solution: Add DescriptionAttribute to the enumerated items and use reflection to obtain Chinese information.
Steps:
1. Add the namespace System. ComponentModel to the class defining the enumeration AuditEnum and add DescriptionAttribute to each enumeration item. The sample code is as follows:
Copy codeThe Code is as follows: using System. ComponentModel;
Public enum AuditEnum
{
[Description ("not submitted for review")]
Holding = 0,
[Description ("under review")]
Auditing = 1,
[Description ("approved")]
Pass = 2,
[Description ("reject")]
Reject = 3
}
2. customize an EnumService. cs class and add the static method GetDescription () to read the Description information based on the incoming enumeration value. The sample code is as follows:
Copy codeThe Code is 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. Add a call to EnumService. GetDescription () to the output enumeration value. The sample code is as follows:
Copy codeThe Code is 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 we assign a simplified value to AuditEnum. Pass.
Audit. Text = EnumService. GetDescription (we );
}
}
The full text is complete.
The above code runs on VS2010. If you have any questions, please leave a message below. If you like it, click here.