Use custom attributes to define detailed texts of enumeration values

Source: Internet
Author: User
New Solution: http://hilite.cnblogs.com/archive/2006/03/28/360793.html

For Enumeration type: enum WorkState
{
/// <Summary>
/// Plan
/// </Summary>
Planing,
/// <Summary>
/// Ready
/// </Summary>
Ready,
/// <Summary>
/// In progress
/// </Summary>
Processing,
/// <Summary>
/// Complete
/// </Summary>
Finished
}

How do you see the enumerated values on the interface? MessageBox. Show (WorkState. Ready. ToString ());

The result must have been "Ready", not "Ready ".

In many cases, the enumerated value text with more complete or other content needs to be displayed,
One solution is to maintain a corresponding table somewhere to maintain all enumerated value texts, which may lead to a disconnection between the enumerated type and text.
Another solution is to define the enumeration type by using the custom attribute "EnumDescription". In this way, the enumerated value and text are together, which makes it easy to maintain. [EnumDescription (WorkState. Planing, "Planning in progress")]
[EnumDescription (WorkState. Ready, "Everything is Ready")]
[EnumDescription (WorkState. Processing, "working in progress")]
[EnumDescription (WorkState. Finished, "Finished")]
Enum WorkState
{
Planing,
Ready,
Processing,
Finished
}

Now you are calling the code: MessageBox. Show (
EnumDescription. GetText (typeof (WorkState), (int) WorkState. Planing ));

You will see the desired text "planning ".

Here is the specific implementation and test code of "EnumDescription. Http://files.cnblogs.com/hilite/EnumDisplayText.zip

Here is the main code: the main code
1/** // <summary>
2 // obtain all the texts defined by the enumerated type
3 /// </summary>
4 /// <exception cref = "NotSupportedException"> </exception>
5 /// <param name = "enumType"> Enumeration type </param>
6 /// <param name = "sortType"> specify the sorting type </param>
7 // <returns> all defined text </returns>
8 public static EnumDescription [] GetTexts (Type enumType, SortType sortType)
9 {
10 EnumDescription [] descriptions = null;
11 // cache Processing
12 if (cachedEnum. Contains (enumType. FullName) = false)
13 cachedEnum. Add (enumType. FullName, enumType. GetCustomAttributes (typeof (EnumDescription), false ));
14 descriptions = (EnumDescription []) cachedEnum [enumType. FullName];
15 if (descriptions. Length <= 0) throw new NotSupportedException ("Enumeration type [" + enumType. Name + "] undefined attribute EnumValueDescription ");
16
17 // sort by the specified attribute bubble
18 for (int m = 0; m <descriptions. Length; m ++)
19 {
20 for (int n = m; n <descriptions. Length; n ++)
21 {
22 EnumDescription temp;
23 bool swap = false;
24
25 switch (sortType)
26 {
27 case SortType. Value:
28 if (descriptions [m]. EnumValue> descriptions [n]. EnumValue) swap = true;
29 break;
30 case SortType. DisplayText:
31 if (string. Compare (descriptions [m]. EnumDisplayText, descriptions [n]. EnumDisplayText)> 0) swap = true;
32 break;
33 case SortType. Rank:
34 if (descriptions [m]. EnumRank> descriptions [n]. EnumRank) swap = true;
35 break;
36}
37
38 if (swap)
39 {
40 temp = descriptions [m];
41 descriptions [m] = descriptions [n];
42 descriptions [n] = temp;
43}
44}
45}
46
47 return descriptions;
48}

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.