示例:
有人為了顯示中文,這樣定義枚舉嗎?
publicenum TimeOfDay
{
上午,
下午,
晚上
};
這樣定義,很別扭,特別是在使用的時候,
比如,this.Time = TimeOfDay.上午;
而且你會逐漸發現它的局限性。
?
枚舉定義很頭疼:
在系統開發中,我們經常使用枚舉,但是定義枚舉是個頭疼的問題。
按照習慣我們習慣將枚舉項定義為英語,但是,在使用的時候,特別針對國內客戶的時候,如果顯示的英文,則不符合要求,不易于用戶使用。
盡管現在枚舉定義也能定義中文枚舉項,但在優雅的英文代碼中穿插著中語,確實很不爽。如果涉及多語,很難擴展。
也有人經常用到常量來代替枚舉,但這種方法在系統開發中不太可取,具體見:枚舉與常量。
?
解決方案:
?
為了方便用戶使用,?希望能夠找到一種比較好的方法,將枚舉轉為我們想要的集合。
枚舉的定義中加入描述,如果要支持多語,則直接修改枚舉描述即可。也不用修改其他代碼。
通過反射思想,得到針對某一枚舉類型的描述。具體實現起來,有如下代碼中的三個不同的的方式。


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結構體的羅列:");foreach (string key in nvc.Keys){Console.WriteLine(string.Format(key + ": {0}", nvc[key]));}Console.WriteLine("\n2. 直接方式1,對TimeOfDay結構體的羅列:");Dictionary<string, string> dic = GetEnumDic(typeof(TimeOfDay));foreach (string key in dic.Keys){Console.WriteLine(key + ":{0}", dic[key]);}Console.WriteLine("\n3. 直接方式2,對TimeOfDay結構體中某一項的描述:");Console.WriteLine(string.Format(TimeOfDay.Moning.ToString() + ":{0}", GetEnumDes(TimeOfDay.Moning)));}/// <summary>/// 從枚舉類型和它的特性讀出并返回一個鍵值對/// </summary>/// <param name="enumType">Type,該參數的格式為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//{// 上午,// 下午,// 晚上//}; }
?
或者通過下載文件,直接進行測試。EnumDecriptionGet.rar
?
?
?
參考文章
?
枚舉顯示中文問題
?
?