When applying enumeration, you often need to convert enumeration and numerical values. Sometimes it is necessary to convert it into a corresponding Chinese character. The following describes a method.
First, create an enumeration:
/// <Summary>
///Color
/// </Summary>
Public EnumColortype
{
/// <Summary>
///Red
/// </Summary>
Red,
/// <Summary>
///Blue
/// </Summary>
Bule,
/// <Summary>
///Green
/// </Summary>
Green
}
Obtain the enumerated value:
IntCode = colortype. Red. gethashcode ();
Obtain the enumerated name with a numerical value:
StringName1 = colortype. Red. tostring ();
//Or
StringName2 = enum. parse (Typeof(Colortype), Code. tostring (). tostring ();
The enumerated name obtained above is in English. If you want to obtain the corresponding Chinese explanation, you can use attribute to implement it,CodeAs follows:
//
// color
//
Public Enum colortype
{< br> ///
// Red
///
[description ( "
Red ")]
Red,
//
/ // blue
///
[description ( " blue " )]
Bule,
//
/ // green
///
[description ( " green " )]
green
}
In enumeration, add description and create a class. You can use the following method to convert enumeration into a corresponding Chinese explanation:
Public Static Class Enumdemo
{
Private Static String Getname (system. type T, Object V)
{
Try
{
Return Enum. getname (T, V );
}
Catch
{
Return " Unknown " ;
}
}
/// <Summary>
/// Returns the description of the specified value of the specified enumeration type.
/// </Summary>
/// <Param name = "T"> Enumeration type </Param>
/// <Param name = "v"> Enumerated Value </Param>
/// <Returns> </returns>
Public Static String Getdescription (system. type T, Object V)
{
Try
{
Fieldinfo ofieldinfo = T. getfield (getname (T, V ));
Descriptionattribute [] attributes = (descriptionattribute []) ofieldinfo. getcustomattributes ( Typeof (Descriptionattribute ), False );
Return (Attributes. length> 0 )? Attributes [ 0 ]. Description: getname (T, V );
}
Catch
{
Return " Unknown " ;
}
}
}
The call method is as follows:
StringName3 = enumdemo. getdescription (Typeof(Colortype), colortype. Red)
What name3 gets is "red ".
When applying enumeration, you often need to convert enumeration and numerical values. Sometimes it is necessary to convert it into a corresponding Chinese character. The following describes a method.
First, create an enumeration:
/// <Summary>
///Color
/// </Summary>
Public EnumColortype
{
/// <Summary>
///Red
/// </Summary>
Red,
/// <Summary>
///Blue
/// </Summary>
Bule,
/// <Summary>
///Green
/// </Summary>
Green
}
Obtain the enumerated value:
IntCode = colortype. Red. gethashcode ();
Obtain the enumerated name with a numerical value:
StringName1 = colortype. Red. tostring ();
//Or
StringName2 = enum. parse (Typeof(Colortype), Code. tostring (). tostring ();
The enumerated name obtained above is in English. If you want to obtain the corresponding Chinese explanation, you can use attribute to implement it. The Code is as follows:
//
// color
//
Public Enum colortype
{< br> ///
// Red
///
[description ( "
Red ")]
Red,
//
/ // blue
///
[description ( " blue " )]
Bule,
//
/ // green
///
[description ( " green " )]
green
}
In enumeration, add description and create a class. You can use the following method to convert enumeration into a corresponding Chinese explanation:
Public Static Class Enumdemo
{
Private Static String Getname (system. type T, Object V)
{
Try
{
Return Enum. getname (T, V );
}
Catch
{
Return " Unknown " ;
}
}
/// <Summary>
/// Returns the description of the specified value of the specified enumeration type.
/// </Summary>
/// <Param name = "T"> Enumeration type </Param>
/// <Param name = "v"> Enumerated Value </Param>
/// <Returns> </returns>
Public Static String Getdescription (system. type T, Object V)
{
Try
{
Fieldinfo ofieldinfo = T. getfield (getname (T, V ));
Descriptionattribute [] attributes = (descriptionattribute []) ofieldinfo. getcustomattributes ( Typeof (Descriptionattribute ), False );
Return (Attributes. length> 0 )? Attributes [ 0 ]. Description: getname (T, V );
}
Catch
{
Return " Unknown " ;
}
}
}
The call method is as follows:
StringName3 = enumdemo. getdescription (Typeof(Colortype), colortype. Red)
What name3 gets is "red ".