C#中Enum,Int,String之间的互相转换

您所在的位置:网站首页 如何把string转成int C#中Enum,Int,String之间的互相转换

C#中Enum,Int,String之间的互相转换

2023-01-16 08:53| 来源: 网络整理| 查看: 265

原文地址 Enum为枚举提供基类,其基础类型可以是除 Char 外的任何整型。如果没有显式声明基础类型,则使用Int32。编程语言通常提供语法来声明由一组已命名的常数和它们的值组成的枚举。 注意:枚举类型的基类型是除 Char 外的任何整型,所以枚举类型的值是整型值。 Enum 提供一些实用的静态方法:

比较枚举类的实例的方法 将实例的值转换为其字符串表示形式的方法 将数字的字符串表示形式转换为此类的实例的方法 创建指定枚举和值的实例的方法。 举例:enum Colors { Red, Green, Blue, Yellow }; Enum->String 利用Object.ToString()方法:如Colors.Green.ToString的值是Green字符串 利用Enum的静态方法GetName与GetNames public static string GetNames(Type enumType,Object value) public static string[] GetNames(Type enumType)

例如: Enum.GetNames(typeof(Colors),3))与Enum.GetName(typeof(Colors),Colors.Bule))的值都是Blue. Enum.GetNames(typeof(Colors))将返回枚举字符串数组.

String->Enum 利用Enum的静态方法Parse: public static Object Parse(Type enumType,string value)

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

Int->Enum 可以强制转换,将整型转换成枚举型 例如:Colors color = (Colors)2,那么color即为Colors.Blue 利用Enum的静态方法ToObject public static Object ToObject(Type enumType,int value)

例如:Colors color = (Colors)Enum.ToObject(typeof(Colors),2),那么color即为Color.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"包含空格呢?我么不能去创建一个包含空格的枚举成员,否则你的代码将不能编译通过,事实上我们有很多方法可以解决这个问题

在枚举成员和字符串之间创建一个映射(可以使用数组或哈希表) 将枚举成员ToString()的结果作为键制定到资源文件 使用反射(我将在下面讲到)

在枚举中使用属性 我将使用属性来达到一个字符串关联到枚举成员的目的,下面通过一个简单的例子来说明这是如何做到的。

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()方法来代替。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3