Get enumeration Description Information (Description)

Source: Internet
Author: User

Get enumeration Description Information (Description)

See other people's enum has a Get enumeration description information (Description), directly to their own add, found not work. Look for a search on the net, originally want this kind of add:

1. Defining an Enum
Using System;
Using System.ComponentModel;

public enum TimeOfDay
{
[Description ("Morning")]
moning = 0,
[Description ("noon")]
Afternoon = 1,
[Description ("Evening")]
Evening = 2,
};


2. Gets the descriptive information for the enum. Return with NameValueCollection.

The key value of the NameValueCollection equals the key of the enum.


Using System;
Using System.Reflection;
Using System.ComponentModel;
Using System.Collections.Specialized;

<summary>
Get the attribute value of an enum
</summary>
<returns></returns>
public static NameValueCollection Convertenumdescriptionvalue ()
{

NameValueCollection NVC = new NameValueCollection ();
Type type = typeof (DescriptionAttribute);

foreach (FieldInfo fi in typeof (Enumclass.timeofday). GetFields ())
{
object[] arr = fi. GetCustomAttributes (type, true);
if (arr. Length > 0)
{

Nvc. Add (FI. Name, ((DescriptionAttribute) arr[0]). Description);
}
}

return NVC;

}

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.