Description of the extension that supports displaying Chinese characters for enumeration types in c #

Source: Internet
Author: User

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.

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.