C# Enum,Int,String的互相转换
Posted WebEnh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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))
![](https://image.cha138.com/20210610/daf1a7c9e215424f9c14913bca315910.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/d99983942134417182719bceb7c3f4d1.jpg)
我们可以通过 ToString() 方法简单地获取到下列信息
![](https://image.cha138.com/20210610/6a7a6fcf7653400699c9026abe8b5aa0.jpg)
![](https://image.cha138.com/20210610/6a7a6fcf7653400699c9026abe8b5aa0.jpg)
![](https://image.cha138.com/20210610/6a7a6fcf7653400699c9026abe8b5aa0.jpg)
![](https://image.cha138.com/20210610/6a7a6fcf7653400699c9026abe8b5aa0.jpg)
![](https://image.cha138.com/20210610/daf1a7c9e215424f9c14913bca315910.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/d99983942134417182719bceb7c3f4d1.jpg)
但我们如何才能获取到的结果类似“Chain Store Manager”包括空格呢?我们不能去创建一个包含空格的枚举成员,否则你的代码将不能编译通过,事实上我们有很多方案可以解决这个问题。
1、在枚举成员和字符串之间创建一个映射(可以使用数组或哈希表)
2、将枚举成员 ToString() 的结果作为为键指定到资源文件
3、使用反射。。。。(我将在下面讲到)
在枚举中使用属性
我将使用属性来达到一个字符串关联到枚举成员的目的,下面通过一个简单的例子来说明这是如何做到的。
![](https://image.cha138.com/20210610/6a7a6fcf7653400699c9026abe8b5aa0.jpg)
![](https://image.cha138.com/20210610/daf1a7c9e215424f9c14913bca315910.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/e0afba9d2a7041aba3acebaf63e76a33.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/7540fb158cfc439c8415535b2fbd63ec.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/e0afba9d2a7041aba3acebaf63e76a33.jpg)
![](https://image.cha138.com/20210610/e0afba9d2a7041aba3acebaf63e76a33.jpg)
![](https://image.cha138.com/20210610/7540fb158cfc439c8415535b2fbd63ec.jpg)
![](https://image.cha138.com/20210610/d99983942134417182719bceb7c3f4d1.jpg)
EnumDescriptionAttribute 类继承自 Attribute,它包含一个类型为String的属性Description,下面将该属性关联到枚举 EmployeeType 的所有成员上。
![](https://image.cha138.com/20210610/6a7a6fcf7653400699c9026abe8b5aa0.jpg)
![](https://image.cha138.com/20210610/daf1a7c9e215424f9c14913bca315910.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/d99983942134417182719bceb7c3f4d1.jpg)
从枚举获取到属性的值
为了获取到属性的值,我必须使用到反射,下是是一个简单的例子:
![](https://image.cha138.com/20210610/6a7a6fcf7653400699c9026abe8b5aa0.jpg)
![](https://image.cha138.com/20210610/6a7a6fcf7653400699c9026abe8b5aa0.jpg)
![](https://image.cha138.com/20210610/6a7a6fcf7653400699c9026abe8b5aa0.jpg)
![](https://image.cha138.com/20210610/6a7a6fcf7653400699c9026abe8b5aa0.jpg)
![](https://image.cha138.com/20210610/6a7a6fcf7653400699c9026abe8b5aa0.jpg)
![](https://image.cha138.com/20210610/6a7a6fcf7653400699c9026abe8b5aa0.jpg)
![](https://image.cha138.com/20210610/6a7a6fcf7653400699c9026abe8b5aa0.jpg)
![](https://image.cha138.com/20210610/6a7a6fcf7653400699c9026abe8b5aa0.jpg)
![](https://image.cha138.com/20210610/6a7a6fcf7653400699c9026abe8b5aa0.jpg)
![](https://image.cha138.com/20210610/6a7a6fcf7653400699c9026abe8b5aa0.jpg)
![](https://image.cha138.com/20210610/6a7a6fcf7653400699c9026abe8b5aa0.jpg)
![](https://image.cha138.com/20210610/6a7a6fcf7653400699c9026abe8b5aa0.jpg)
![](https://image.cha138.com/20210610/6a7a6fcf7653400699c9026abe8b5aa0.jpg)
![](https://image.cha138.com/20210610/6a7a6fcf7653400699c9026abe8b5aa0.jpg)
![](https://image.cha138.com/20210610/6a7a6fcf7653400699c9026abe8b5aa0.jpg)
![](https://image.cha138.com/20210610/daf1a7c9e215424f9c14913bca315910.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/c5e831cb3fe94c4ba4154e352bd073fe.jpg)
![](https://image.cha138.com/20210610/d99983942134417182719bceb7c3f4d1.jpg)
其中最重点的一行代码:FieldInfo fieldInfo = employee.GetType().GetField("ChainStoreManager"); 我们注意硬编码到里面的枚举成员名称ChainStoreManager实际上可以通过 ToString() 方法来代替。
以上是关于C# Enum,Int,String的互相转换的主要内容,如果未能解决你的问题,请参考以下文章