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