枚举转中文,通过反射方法与描述的方式获取
示例:
有人為了顯示中文,這樣定義枚舉嗎?
publicenum TimeOfDay{
上午,
下午,
晚上
};
這樣定義,很別扭,特別是在使用的時候,
比如,this.Time = TimeOfDay.上午;
而且你會逐漸發(fā)現(xiàn)它的局限性。
?
枚舉定義很頭疼:
在系統(tǒng)開發(fā)中,我們經(jīng)常使用枚舉,但是定義枚舉是個頭疼的問題。
按照習(xí)慣我們習(xí)慣將枚舉項定義為英語,但是,在使用的時候,特別針對國內(nèi)客戶的時候,如果顯示的英文,則不符合要求,不易于用戶使用。
盡管現(xiàn)在枚舉定義也能定義中文枚舉項,但在優(yōu)雅的英文代碼中穿插著中語,確實很不爽。如果涉及多語,很難擴(kuò)展。
也有人經(jīng)常用到常量來代替枚舉,但這種方法在系統(tǒng)開發(fā)中不太可取,具體見:枚舉與常量。
?
解決方案:
?
為了方便用戶使用,?希望能夠找到一種比較好的方法,將枚舉轉(zhuǎn)為我們想要的集合。
枚舉的定義中加入描述,如果要支持多語,則直接修改枚舉描述即可。也不用修改其他代碼。
通過反射思想,得到針對某一枚舉類型的描述。具體實現(xiàn)起來,有如下代碼中的三個不同的的方式。
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Reflection; using System.ComponentModel;namespace EnumApp {class Program{static void Main(string[] args){NameValueCollection nvc = GetNVCFromEnumValue(typeof(TimeOfDay));Console.WriteLine("1. 反射方式對TimeOfDay結(jié)構(gòu)體的羅列:");foreach (string key in nvc.Keys){Console.WriteLine(string.Format(key + ": {0}", nvc[key]));}Console.WriteLine("\n2. 直接方式1,對TimeOfDay結(jié)構(gòu)體的羅列:");Dictionary<string, string> dic = GetEnumDic(typeof(TimeOfDay));foreach (string key in dic.Keys){Console.WriteLine(key + ":{0}", dic[key]);}Console.WriteLine("\n3. 直接方式2,對TimeOfDay結(jié)構(gòu)體中某一項的描述:");Console.WriteLine(string.Format(TimeOfDay.Moning.ToString() + ":{0}", GetEnumDes(TimeOfDay.Moning)));}/// <summary>/// 從枚舉類型和它的特性讀出并返回一個鍵值對/// </summary>/// <param name="enumType">Type,該參數(shù)的格式為typeof(需要讀的枚舉類型)</param>/// <returns>鍵值對</returns>public static NameValueCollection GetNVCFromEnumValue(Type enumType){System.Reflection.FieldInfo[] fields;string strText, strValue;NameValueCollection nvc = new NameValueCollection();Type typeDescription = typeof(DescriptionAttribute);fields = enumType.GetFields();foreach (FieldInfo field in fields){if (field.FieldType.IsEnum){strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();object[] arr = field.GetCustomAttributes(typeDescription, true);if (arr.Length > 0){DescriptionAttribute aa = (DescriptionAttribute)arr[0];strText = aa.Description;}else{strText = field.Name;}nvc.Add(strValue, strText);}}return nvc;}/// <summary>/// 返回 Dic<枚舉項,描述>/// </summary>/// <param name="enumType"></param>/// <returns>Dic<枚舉項,描述></returns>public static Dictionary<string, string> GetEnumDic(Type enumType){Dictionary<string, string> dic = new Dictionary<string, string>();FieldInfo[] fieldinfos = enumType.GetFields();foreach (FieldInfo field in fieldinfos){if (field.FieldType.IsEnum){Object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);dic.Add(field.Name, ((DescriptionAttribute)objs[0]).Description);}}return dic;}/// <summary>/// 獲得某個枚舉項的描述/// </summary>/// <param name="value"></param>/// <returns></returns>public static string GetEnumDes(object value){FieldInfo fieldinfo = value.GetType().GetField(value.ToString());Object[] objs = fieldinfo.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);if (objs == null || objs.Length == 0){return value.ToString();}else{return ((DescriptionAttribute)objs[0]).Description;}}}public enum TimeOfDay{[Description("上午")]Moning = 0,[Description("下午")]Afternoon,[Description("晚上")]Evening,};//public enum TimeOfDays//{// 上午,// 下午,// 晚上//}; } View Code?
或者通過下載文件,直接進(jìn)行測試。EnumDecriptionGet.rar
?
?
?
參考文章
?
枚舉顯示中文問題
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/arxive/p/6285718.html
總結(jié)
以上是生活随笔為你收集整理的枚举转中文,通过反射方法与描述的方式获取的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c/c++连接mysql数据库设置及乱码
- 下一篇: HTTP服务器的本质:tinyhttpd