枚举中文转换为英文及调用

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 = 12
44         }

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 = string.Empty;
 81             foreach (FieldInfo field in fields)
 82             {
 83                 if (field.FieldType.IsEnum)
 84                 {
 85                     strValue = ((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         }

时间: 2024-08-09 14:38:43

枚举中文转换为英文及调用的相关文章

JAVAWEB项目实现验证码中文、英文、数字组合

验证码基础 一.什么是验证码及它的作用 :验证码为全自动区分计算机和人类的图灵测试的缩写,是一种区分用户是计算机的公共全自动程序,这个问题可以由计算机生成并评判,但是必须只有人类才能解答.可以防止恶意破解密码.刷票.论坛灌水.有效防止某个黑客对某一个特定注册用户用特定程序暴力破解方式进行不断的登录. 二.图文验证码的原理 :在servlet中随机生成一个指定位置的验证码,一般为四位,然后把该验证码保存到session中.在通过Java的绘图类以图片的形式输出该验证码.为了增加验证码的安全级别,可

Newtonsoft.Json高级用法之枚举中文转义

最近看博客园中 焰尾迭的两篇关于"Newtonsoft.Json高级用法"的文章受到了很多人的评论,一度登入到头条推荐. 今天我就不再重复焰尾迭博文中的一些提过的Newtonsoft.Json的高级用法.大家如果想知道直接去看. Newtonsoft.Json高级用法 再谈Newtonsoft.Json高级用法 我主要说焰尾迭没有提到的用法——枚举中文转义 枚举值序列化问题(摘自焰尾迭段落) public enum NotifyType { /// <summary> //

【转】正则表达式 匹配中文,英文字母和数字及_的写法!同时控制长度

匹配中文:[\u4e00-\u9fa5] 英文字母:[a-zA-Z] 数字:[0-9] 匹配中文,英文字母和数字及_: ^[\u4e00-\u9fa5_a-zA-Z0-9]+$ 同时判断输入长度:[\u4e00-\u9fa5_a-zA-Z0-9_]{4,10} ^[\w\u4E00-\u9FA5\uF900-\uFA2D]*$ 1.一个正则表达式,只含有汉字.数字.字母.下划线不能以下划线开头和结尾:^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$  其中:^

把int型非负数转换为英文

数字转换为英文 输入为int型非负数,最大值为2^31 - 1 = 2 147 483 647 输出为String英文,最大输出为Two Billion One Hundred Forty Seven Million Four Hundred Eighty Three Thousand Six Hundred Forty Seven 输出要求每个单词首字母大写,之间用空格隔开,不需要用“and”做连词 例如: 123 -> "One Hundred Twenty Three" 1

ORACLE中将数字转换为英文

SELECT LEVEL, to_char(to_date(LEVEL,'J'),'Jsp') FROM dual CONNECT BY LEVEL <= 1000 运行结果如下图所示:   说明: TO_CHAR(aDate,'JSP')是指日期aDate距离JULIAN日期的第一天即4712-01-01BC的天数,也就是从公元前4712年的1月1号到aDate这个日期的天数的英文拼写. JULIAN日期的范围是公元前4712-01-01到公元9999-01-01.期间的天数是我们能够拼写的最

Js控制文本框只能输入中文、英文、数字与指定特殊符号

JS 控制文本框只能输入数字<input onkeyup="value=value.replace(/[^0-9]/g,'')"onpaste="value=value.replace(/[^0-9]/g,'')" oncontextmenu ="value=value.replace(/[^0-9]/g,'')"> JS 控制文本框只能输入数字.小数点<inputonkeyup="value=value.replac

PHP如何将中文转换为拼音

用来得到中文的首字母: 这个是将中文转换为拼音的类:charset <?php/*** 汉字转化为拼音,拼音转化为汉字**/ class charset{private $_code=array(array("a",'-20319'),array("ai",'-20317'),array("an",'-20304'),array("ang",'-20295'),array("ao",'-20292')

利用Pinyin4j把中文转换为拼音

原文:利用Pinyin4j把中文转换为拼音 源代码下载地址:http://www.zuidaima.com/share/1550463764974592.htm 利用Pinyin4j把中文转换为拼音: package com.zuidaima; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; import net.source

在数据库中将中文转换为拼音或者汉字首字母 转

sql数据库自定义一个函数把下面代码写进去 功能是得到汉字拼音首字母 如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 create function fun_getPY(@str nvarchar(4000)) returns nvarchar(4000) as begin declare @word n