C # enumeration Conversion

Source: Internet
Author: User

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 ".

 

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.