Change uncertainty to confirmation ~ Can the description be obtained by enumeration of the Flag feature?

Source: Internet
Author: User

Back to directory

I have previously written a method for outputting description feature information for objects of the common Enumeration type. Today I will focus on how to implement the description information of the flags enumeration element in the bit field.

For a common enumeration object, the output description is as follows:

  Public   Enum   Status {[description ( "   normal  "  )] Normal  =- 1  , [description ( "   Delete  "  )] deletet  =  0  , [description ( "   freeze  "  )] freezed  =  1  ,} 
Status status =Status. Normal; console. writeline (status. getdescription ());

For enumeration that supports the bit field flags featureGetdescriptionMethods cannot be implemented. We need to make some modifications to them.

 [Flags]  Public   Enum  Ball {[description (  "  Football  "  )] Football = 1  , [Description (  "  Basketball "  )] Basketball = 2  , [Description (  "  Table Tennis  "  )] Pingpang = 4  ,} 
     ///   <Summary>      ///  Enumeration type Extension Method  ///   </Summary>     Public   Static   Class  Enumextensions {  ///   <Summary>          ///  Obtains the enumeration set of the flags feature.  ///   </Summary>          ///   <Param name = "value"> </param>          ///   <Returns> </returns>          Static List <Enum>Getenumvaluesfromflagsenum (Enum value) {list <Enum> values = enum. getvalues (value. GetType (). Cast <Enum> (). Tolist (); List <Enum> res = New List <Enum> ();  Foreach ( VaR Itemvalue In  Values ){  If (Value. gethashcode () & itemvalue. gethashcode ())! = 0 ) Res. Add (itemvalue );}  Return  Res ;}  ///   <Summary>            ///  Obtains the description attribute of the enumerated variable value.  ///   </Summary>            ///   <Param name = "OBJ">  Enumerated variable  </Param>            ///   <Returns> If the description attribute is included, the value of the description attribute is returned. Otherwise, the name of the enumerated variable value is returned.  </Returns>            Public   Static   String Getdescription ( This  Enum OBJ ){  String Description = String  . Empty;  Try  {Type _ enumtype = OBJ. GetType (); descriptionattribute DNA = Null  ; Fieldinfo fi = Null  ;  VaR Fields = _ Enumtype. getcustomattributesdata ();  If (! Fields. Where (I => I. constructor. declaringtype. Name = "  Flagsattribute  "  ). Any () {fi =_ Enumtype. getfield (enum. getname (_ enumtype, OBJ); DNA = (Descriptionattribute) attribute. getcustomattribute (FI, Typeof  (Descriptionattribute ));  If (DNA! = Null && String . Isnullorempty (DNA. Description) = False  )  Return  DNA. description;  Return  Null  ;} Getenumvaluesfromflagsenum (OBJ). tolist (). foreach (I => {Fi = _ Enumtype. getfield (enum. getname (_ enumtype, I); DNA = (Descriptionattribute) attribute. getcustomattribute (FI, Typeof  (Descriptionattribute ));  If (DNA! = Null && String . Isnullorempty (DNA. Description) = False ) Description + = DNA. Description + "  ,  "  ;});  Return Description. endswith ( "  ,  "  ) ? Description. Remove (description. lastindexof ( '  ,  ' ): Description ;}  Catch  {  Throw  ;}}} 

After it is assigned a value, the output is as follows:

 
Ball ball = ball. Basketball | ball. Football;
Console. writeline (ball. getdescription ());

Back to directory

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.