[Accumulate by bit] add more information for enumeration through the feature

Source: Internet
Author: User

Attribute is a method of associating additional data to an Attribute (and other constructor), while enumeration is the most commonly used structure in programming, enumeration is essentially a constant value. Compared to using these values directly, enumeration provides us with better readability. We know that the basic types of enumeration can only be values (byte, sbyte, short, ushort, int, uint, long, or ulong). In general, enumeration can meet our needs, however, sometimes we need to add more information to the enumeration, but it is not enough to only use these value types. In this case, we can apply the features of the enumeration type to make the enumeration more information.

Use DescriptionAttribute in Enumeration

First, introduce: namespace. The following is an enumeration application feature:

               [Description(          [Description(          [Description(      }

The following is an extension method for obtaining the Description feature:

                                                                       GetDescription<TEnum>(              TEnum :               Type type =              (!                    ArgumentException(,                             MemberInfo[] memberInfo =              (memberInfo !=  && memberInfo.Length >                   [] attrs = memberInfo[].GetCustomAttributes((DescriptionAttribute),                    (attrs !=  && attrs.Length >                                             ((DescriptionAttribute)attrs[                                     }

 

Finally, we can use this extension method to obtain the descriptive information of this enumeration:

        Main(                    description =     }

Related Article

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.