[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 type of enumeration can only be value type (Byte,Sbyte,Short,Ushort,Int,Uint,LongOrUlong), in general, enumeration can meet our needs, but sometimes we need to add more information for enumeration, just using these value types is not enough, in this case, you can apply the features of the enumeration type to bring more information to the enumeration.

Use descriptionattribute in Enumeration

First introduce:Using system. componentmodelNamespace. The following is an enumeration application.DescriptionattributeFeatures:

 1       Enum  Fruit  2   {  3 [Description ( "  Apple  "  )]  4   Apple,  5 [Description ( "  Orange  "  )]  6   Orange,  7 [Description ( "  Watermelon "  )]  8   Watermelon  9 }

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

 1           ///   <Summary>  2           ///  Obtains enumeration description feature values.  3           ///   </Summary>  4           ///  <Typeparam name = "tenum"> </typeparam>  5           ///   <Param name = "enumerationvalue">  Enumerated Value  </Param>  6           ///   <Returns>  Description of enumerated values/returns>  7           Public   Static   String Getdescription <tenum> ( This Tenum enumerationvalue)  8               Where Tenum: Struct  , Icomparable, iformattable, iconvertible  9   {  10 Type type = Enumerationvalue. GetType ();  11               If (! Type. isenum)  12   {  13                  Throw   New Argumentexception ( "  Enumerationvalue must be an enumerated value.  " , "  Enumerationvalue  "  );  14   }  15   16               //  Use reflection to obtain the member information of this enumeration.  17 Memberinfo [] memberinfo = Type. getmember (enumerationvalue. tostring ());  18               If (Memberinfo! = Null & Amp; memberinfo. Length & gt; 0  )  19   {  20                   Object [] Attrs = memberinfo [ 0 ]. Getcustomattributes ( Typeof (Descriptionattribute ), False );  21   22                   If (Attrs! = Null & Amp; attrs. Length & gt; 0  )  23   {  24                       //  Return enumeration value description  25                       Return (Descriptionattribute) attrs [ 0 ]). Description;  26   }  27   }  28               //  If no feature value is described, return the string format of this enumeration.  29               Return  Enumerationvalue. tostring ();  30 }

 

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

  1   Public   static   void  main ( string   [] ARGs)   2  {  3  ///   description =" orange "  4   string  description =  fruit. orange. getdescription ();   5 }

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.