1 namespace ConsoleApplication8 2 { 3 enum orientation : byte 4 { 5 north=0, 6 south=1, 7 east=2, 8 west=3 9 } 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 byte directionByte; 15 string directionString; 16 orientation myDirection; 17 18 //获取枚举的字符串 19 myDirection = orientation.north; 20 Console.WriteLine("myDirection={0}",myDirection);//north 21 22 23 //将枚举的字符串变为字符串所对应的值 24 directionByte = (byte)myDirection; 25 //将枚举类型变量转换成字符串类型,用这种方式转换是因为用(string)进行强制类型转换是行不通的, 26 //但可以使用变量本身进行ToString()转换也是可以的,与使用Convert.ToString()效果相同。 27 directionString = Convert.ToString(myDirection); 28 Console.WriteLine("byte equivalent={0}:",directionByte);//0 29 Console.WriteLine("string equivalent={0}:" ,directionString);//north 30 31 32 //将byte数字转换成对应的枚举值(强制转换) 33 //(,注意:并不是所有的byte类型变量值都可以映射为已经定义的orientation值,orientation类型可以存储其他byte值, 34 //所以不会直接产生错误,但会在应用程序的后面违反逻辑。) 35 byte myByte = 1; 36 myDirection =(orientation)myByte; 37 Console.WriteLine("byte Outside={0}",myDirection);//south 38 39 40 //把string转换成枚举值,语法:Enum.Parse();用法如下: 41 //(enumerationType)Enum.Parse(typeof(enumerationType),enumerationValueString); 42 string myString = "east"; 43 myDirection = (orientation)Enum.Parse(typeof(orientation), myString); 44 byte b = (byte)myDirection; 45 Console.WriteLine(myDirection);//east 46 Console.WriteLine(b);//2 47 48 Console.ReadKey(); 49 } 50 } 51 }
对枚举类型进行描述:
定义枚举类型
1 using System.ComponentModel; 2 3 namespace HYZT.Ltxy 4 { 5 /// <summary> 6 /// 行程类型 7 /// </summary> 8 public enum TripType : byte 9 { 10 /// <summary> 11 /// 单程:One Way 12 /// </summary> 13 [Description("单程")] 14 OW = 1, 15 16 /// <summary> 17 /// 往返:Round Trip 18 /// </summary> 19 [Description("往返")] 20 RT = 2, 21 22 /// <summary> 23 /// 两段:two segments 24 /// </summary> 25 [Description("两段")] 26 TS = 3, 27 28 /// <summary> 29 /// 多段:multi segment 30 /// </summary> 31 [Description("多段")] 32 MS = 4 33 } 34 }
枚举类型帮助类:
对描述信息进行操作:
1 using System; 2 using System.ComponentModel; 3 using System.Reflection; 4 5 namespace VariFlightModule 6 { 7 public static class EnumHelper 8 { 9 /// <summary> 10 /// 返回枚举项的描述信息 11 /// </summary> 12 /// <param name="value">要获取描述信息的枚举项</param> 13 /// <param name="nameInstead">当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认true使用</param> 14 /// <returns>枚举想的描述信息</returns> 15 public static string GetDescription(this Enum value, bool nameInstead = true) 16 { 17 Type enumType = value.GetType(); 18 DescriptionAttribute attr = null; 19 20 // 获取枚举常数名称。 21 string name = Enum.GetName(enumType, value); 22 if (name == null) 23 { 24 return null; 25 } 26 // 获取枚举字段。 27 FieldInfo fieldInfo = enumType.GetField(name); 28 if (fieldInfo != null) 29 { 30 // 获取描述的属性。 31 attr = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute; 32 } 33 34 if (attr == null && nameInstead) 35 return name; 36 37 return attr == null ? null : attr.Description; 38 39 } 40 41 /// <summary> 42 /// 获取字段Description 43 /// </summary> 44 /// <param name="fieldInfo"></param> 45 /// <returns></returns> 46 public static DescriptionAttribute[] GetDescriptAttr(this FieldInfo fieldInfo) 47 { 48 if (fieldInfo != null) 49 { 50 return (DescriptionAttribute[]) fieldInfo.GetCustomAttributes(typeof (DescriptionAttribute), false); 51 } 52 return null; 53 } 54 55 /// <summary> 56 /// 根据Description获取枚举 57 /// </summary> 58 /// <typeparam name="T">枚举类型</typeparam> 59 /// <param name="description">枚举描述</param> 60 /// <param name="enumName">枚举值</param> 61 /// <returns>true:成功 false:失败</returns> 62 public static bool TryGetEnumName<T>(string description, out T enumName) 63 { 64 Type _type = typeof (T); 65 foreach (FieldInfo field in _type.GetFields()) 66 { 67 DescriptionAttribute[] _curDesc = field.GetDescriptAttr(); 68 if (_curDesc != null && _curDesc.Length > 0) 69 { 70 if (_curDesc[0].Description == description) 71 { 72 enumName = (T) field.GetValue(null); 73 return true; 74 } 75 } 76 else 77 { 78 if (field.Name == description) 79 { 80 enumName = (T) field.GetValue(null); 81 return true; 82 } 83 } 84 } 85 enumName = default(T); 86 return false; 87 } 88 } 89 }