C#枚舉轉換

來源:互聯網
上載者:User

在應用枚舉的時候,時常需要將枚舉和數值相互轉換的情況。有時候還需要轉換成相應的中文。下面介紹一種方法。

首先建立一個枚舉:

 
    /// <summary>
/// 顏色
/// </summary>
public enum ColorType
{
/// <summary>
/// 紅色
/// </summary>
Red,

/// <summary>
/// 藍色
/// </summary>
Bule,

/// <summary>
/// 綠色
/// </summary>
Green
}
 

 

獲得枚舉數值:

 int code = ColorType.Red.GetHashCode();

有數值獲得枚舉名稱:

string name1=ColorType.Red.ToString();
//或者
string name2= Enum.Parse(typeof(ColorType), code.ToString()).ToString();

以上獲得的枚舉名稱,是英文,如果要獲得相應的中文解釋,可以利用Attribute來實現,代碼如下:

 
  /// <summary>
/// 顏色
/// </summary>
public enum ColorType
{
/// <summary>
/// 紅色
/// </summary>
[Description("紅色")]
Red,

/// <summary>
/// 藍色
/// </summary>
[Description("藍色")]
Bule,

/// <summary>
/// 綠色
/// </summary>
[Description("綠色")]
Green
}
 

 

在枚舉中,加入Description,然後建立一個類,有如下方法用來把枚舉轉換成對應的中文解釋:

 
 public static class EnumDemo
{
private static string GetName(System.Type t, object v)
{
try
{
return Enum.GetName(t, v);
}
catch
{
return "UNKNOWN";
}
}

/// <summary>
/// 返回指定枚舉類型的指定值的描述
/// </summary>
/// <param name="t">枚舉類型</param>
/// <param name="v">枚舉值</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";
}
}
}
 

調用方法如下:

string name3=EnumDemo.GetDescription(typeof(ColorType), ColorType.Red)

name3得到的就是“紅色”。

 

   

在應用枚舉的時候,時常需要將枚舉和數值相互轉換的情況。有時候還需要轉換成相應的中文。下面介紹一種方法。

首先建立一個枚舉:

 
    /// <summary>
/// 顏色
/// </summary>
public enum ColorType
{
/// <summary>
/// 紅色
/// </summary>
Red,

/// <summary>
/// 藍色
/// </summary>
Bule,

/// <summary>
/// 綠色
/// </summary>
Green
}
 

 

獲得枚舉數值:

 int code = ColorType.Red.GetHashCode();

有數值獲得枚舉名稱:

string name1=ColorType.Red.ToString();
//或者
string name2= Enum.Parse(typeof(ColorType), code.ToString()).ToString();

以上獲得的枚舉名稱,是英文,如果要獲得相應的中文解釋,可以利用Attribute來實現,代碼如下:

 
  /// <summary>
/// 顏色
/// </summary>
public enum ColorType
{
/// <summary>
/// 紅色
/// </summary>
[Description("紅色")]
Red,

/// <summary>
/// 藍色
/// </summary>
[Description("藍色")]
Bule,

/// <summary>
/// 綠色
/// </summary>
[Description("綠色")]
Green
}
 

 

在枚舉中,加入Description,然後建立一個類,有如下方法用來把枚舉轉換成對應的中文解釋:

 
 public static class EnumDemo
{
private static string GetName(System.Type t, object v)
{
try
{
return Enum.GetName(t, v);
}
catch
{
return "UNKNOWN";
}
}

/// <summary>
/// 返回指定枚舉類型的指定值的描述
/// </summary>
/// <param name="t">枚舉類型</param>
/// <param name="v">枚舉值</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";
}
}
}
 

調用方法如下:

string name3=EnumDemo.GetDescription(typeof(ColorType), ColorType.Red)

name3得到的就是“紅色”。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.