首页 > 代码库 > 枚举中文转换为英文及调用
枚举中文转换为英文及调用
1.枚举
1 /// <summary> 2 /// 语言类别 3 /// </summary> 4 public enum LanguageType 5 { 6 [Description("所有语言")] 7 All = 0, 8 9 [Description("中文")]10 cn = 1,11 12 [Description("英文")]13 en = 2,14 15 [Description("印度斯坦语")]16 Hindustani = 3,17 18 [Description("西班牙语")]19 Spanish = 4,20 21 [Description("俄语")]22 Russian = 5,23 24 [Description("阿拉伯语")]25 Arabic = 6,26 27 [Description("孟加拉语")]28 Bengali = 7,29 30 [Description("葡萄牙语")]31 Portuguese = 8,32 33 [Description("马来-印尼语")]34 MalayIndonesian = 9,35 36 [Description("法语")]37 French = 10,38 39 [Description("日语")]40 Japanese = 11,41 42 [Description("韩语")]43 Korean = 1244 }
2.方法
1 public class EnumHelp 2 { 3 public static string GetDescription(Enum obj) 4 { 5 string objName = obj.ToString(); 6 Type t = obj.GetType(); 7 FieldInfo fi = t.GetField(objName); 8 DescriptionAttribute[] arrDesc = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); 9 10 return arrDesc[0].Description; 11 } 12 13 /// <summary> 14 /// 获取枚举值的详细文本 15 /// </summary> 16 /// <param name="e"></param> 17 /// <returns></returns> 18 public static string GetEnumDescription(object e) 19 { 20 //获取字段信息 21 System.Reflection.FieldInfo[] ms = e.GetType().GetFields(); 22 23 Type t = e.GetType(); 24 foreach (System.Reflection.FieldInfo f in ms) 25 { 26 //判断名称是否相等 27 if (f.Name != e.ToString()) continue; 28 29 //反射出自定义属性 30 foreach (Attribute attr in f.GetCustomAttributes(true)) 31 { 32 //类型转换找到一个Description,用Description作为成员名称 33 System.ComponentModel.DescriptionAttribute dscript = attr as System.ComponentModel.DescriptionAttribute; 34 if (dscript != null) 35 return dscript.Description; 36 } 37 38 } 39 40 //如果没有检测到合适的注释,则用默认名称 41 return e.ToString(); 42 } 43 44 45 ///<summary> 46 /// 返回 Dic<枚举项,描述> 47 ///</summary> 48 ///<param name="enumType"></param> 49 ///<returns>Dic<枚举项,描述></returns> 50 public static Dictionary<string, string> GetEnumDic(Type enumType) 51 { 52 Dictionary<string, string> dic = new Dictionary<string, string>(); 53 FieldInfo[] fieldinfos = enumType.GetFields(); 54 foreach (FieldInfo field in fieldinfos) 55 { 56 if (field.FieldType.IsEnum) 57 { 58 Object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false); 59 60 dic.Add(field.Name, ((DescriptionAttribute)objs[0]).Description); 61 } 62 63 } 64 65 return dic; 66 } 67 68 69 ///<summary> 70 /// 从枚举类型和它的特性读出并返回一个键值对 71 ///</summary> 72 ///<param name="enumType">Type,该参数的格式为typeof(需要读的枚举类型)</param> 73 ///<returns>键值对</returns> 74 public static NameValueCollection GetNVCFromEnumValue(Type enumType) 75 { 76 NameValueCollection nvc = new NameValueCollection(); 77 Type typeDescription = typeof(DescriptionAttribute); 78 System.Reflection.FieldInfo[] fields = enumType.GetFields(); 79 string strText = string.Empty; 80 string strValue = http://www.mamicode.com/string.Empty; 81 foreach (FieldInfo field in fields) 82 { 83 if (field.FieldType.IsEnum) 84 { 85 strValue = http://www.mamicode.com/((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString(); 86 object[] arr = field.GetCustomAttributes(typeDescription, true); 87 if (arr.Length > 0) 88 { 89 DescriptionAttribute aa = (DescriptionAttribute)arr[0]; 90 strText = aa.Description; 91 } 92 else 93 { 94 strText = field.Name; 95 } 96 nvc.Add(strValue, strText); 97 } 98 } 99 return nvc;100 }101 }
3.调用
1 protected string GetLanguageTypeName(int languageValue) {2 Dictionary<string,string> dic = EnumHelp.GetEnumDic(typeof(CommonEnum.LanguageType));3 string name = Enum.GetName(typeof(HX.Common.CommonEnum.LanguageType), languageValue);4 5 return dic[name];6 }
枚举中文转换为英文及调用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。