国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C# Enum,Int,String的互相轉換

2019-11-17 02:21:12
字體:
來源:轉載
供稿:網友
C# Enum,Int,String的互相轉換

Enum為枚舉提供基類,其基礎類型可以是除 Char 外的任何整型。如果沒有顯式聲明基礎類型,則使用Int32。編程語言通常提供語法來聲明由一組已命名的常數和它們的值組成的枚舉。

注意:枚舉類型的基類型是除 Char 外的任何整型,所以枚舉類型的值是整型值。

Enum 提供一些實用的靜態方法:

(1)比較枚舉類的實例的方法

(2)將實例的值轉換為其字符串表示形式的方法

(3)將數字的字符串表示形式轉換為此類的實例的方法

(4)創建指定枚舉和值的實例的方法。

舉例:enum Colors { Red, Green, Blue, Yellow };

Enum-->String

(1)利用Object.ToString()方法:如Colors.Green.ToString()的值是"Green"字符串;

(2)利用Enum的靜態方法GetName與GetNames:

public static string GetName(Type enumType,Object value)

public static string[] GetNames(Type enumType)

例如:Enum.GetName(typeof(Colors),3))與Enum.GetName(typeof(Colors), Colors.Blue))的值都是"Blue"

Enum.GetNames(typeof(Colors))將返回枚舉字符串數組。

String-->Enum

(1)利用Enum的靜態方法Parse:

public static Object Parse(Type enumType,string value)

例如:(Colors)Enum.Parse(typeof(Colors), "Red")

Enum-->Int

(1)因為枚舉的基類型是除 Char 外的整型,所以可以進行強制轉換。

例如:(int)Colors.Red, (byte)Colors.Green

Int-->Enum

(1)可以強制轉換將整型轉換成枚舉類型。

例如:Colors color = (Colors)2 ,那么color即為Colors.Blue

(2)利用Enum的靜態方法ToObject。

public static Object ToObject(Type enumType,int value)

例如:Colors color = (Colors)Enum.ToObject(typeof(Colors), 2),那么color即為Colors.Blue

判斷某個整型是否定義在枚舉中的方法:Enum.IsDefined

public static bool IsDefined(Type enumType,Object value)

例如:Enum.IsDefined(typeof(Colors), n))

public enum EmployeeType

{

RegularEmployee,

StoreManager,

ChainStoreManager,

DepartmentManager,

Supervisor

}

我們可以通過 ToString() 方法簡單地獲取到下列信息

EmployeeType employee = EmployeeType.ChainStoreManager;

Console.WriteLine(employee.ToString());

Console.WriteLine(EmployeeType.ChainStoreManager.ToString());

/*

輸出結果:

ChainStoreManager

ChainStoreManager

*/

但我們如何才能獲取到的結果類似“Chain Store Manager”包括空格呢?我們不能去創建一個包含空格的枚舉成員,否則你的代碼將不能編譯通過,事實上我們有很多方案可以解決這個問題。

1、在枚舉成員和字符串之間創建一個映射(可以使用數組或哈希表)

2、將枚舉成員 ToString() 的結果作為為鍵指定到資源文件

3、使用反射。。。。(我將在下面講到)

在枚舉中使用屬性

我將使用屬性來達到一個字符串關聯到枚舉成員的目的,下面通過一個簡單的例子來說明這是如何做到的。

public class EnumDescriptionAttribute : Attribute

{

PRivate string m_strDescription;

public EnumDescriptionAttribute(string strPrinterName)

{

m_strDescription = strPrinterName;

}

public string Description

{

get { return m_strDescription; }

}

}

EnumDescriptionAttribute 類繼承自 Attribute,它包含一個類型為String的屬性Description,下面將該屬性關聯到枚舉 EmployeeType 的所有成員上。

public enum EmployeeType

{

[EnumDescription("Regular Employee")]

RegularEmploye,

[EnumDescription("Store Manager")]

StoreManager,

[EnumDescription("Chain Store Manager")]

ChainStoreManager,

[EnumDescription("Department Manager")]

DepartmentManager,

[EnumDescription("On Floor Supervisor")]

Supervisor

}

從枚舉獲取到屬性的值

為了獲取到屬性的值,我必須使用到反射,下是是一個簡單的例子:

// setup the enum

EmployeeType employee = EmployeeType.ChainStoreManager;

// get the field informaiton

FieldInfo fieldInfo = employee.GetType().GetField("ChainStoreManager");

// get the attributes for the enum field

object[] attribArray = fieldInfo.GetCustomAttributes(false);

// cast the one and only attribute to EnumDescriptionAttribute

EnumDescriptionAttribute attrib = (EnumDescriptionAttribute)attribArray[0];

// write the description

console.WriteLine("Description: {0}", attrib.Description);

/*

輸入結果:

Chain Store Manager

*/

其中最重點的一行代碼:FieldInfo fieldInfo = employee.GetType().GetField("ChainStoreManager"); 我們注意硬編碼到里面的枚舉成員名稱ChainStoreManager實際上可以通過 ToString() 方法來代替。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 湖北省| 旬阳县| 元江| 拉萨市| 武胜县| 额济纳旗| 剑川县| 汤原县| 禹城市| 息烽县| 丁青县| 万州区| 孟州市| 当涂县| 旅游| 云浮市| 长宁区| 仙桃市| 江城| 湖南省| 同心县| 海林市| 长丰县| 黎川县| 新平| 澜沧| 蓬安县| 沙田区| 玉树县| 广水市| 天津市| 宜章县| 安义县| 皋兰县| 灵石县| 博爱县| 山阳县| 娄底市| 忻城县| 当涂县| 蒲城县|