public static IDictionary <string, string> ToNDescriptionDictionary(Type enumType) { var dictionary = new Dictionary <string, string>(); object enumInstance = enumType.Assembly.CreateInstance(enumType.FullName); FieldInfo[] fieldInfos = enumType.GetFields(); foreach (FieldInfo fieldInfo in fieldInfos) { var descriptionAttributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), true); if (descriptionAttributes.Length > 0) { var key = ((int)fieldInfo.GetValue(enumInstance)).ToString(); var value = descriptionAttributes[0].Description; if (!dictionary.ContainsKey(key)) { dictionary.Add(key, value); } } } var result = new OptimizedDictionary <string, string>(); foreach (var kvp in dictionary) { result.Add(kvp.Key, kvp.Value); } return(result); }
/// <summary> /// /// </summary> /// <param name="enumType"></param> /// <returns></returns> public static IDictionary <string, string> ToDescriptionDictionary(Type enumType) { string cacheKey = "CACHE_ENUM_KEYVALUE_" + enumType.FullName.ToUpper(); var dictionary = HttpContext.Current.Cache[cacheKey] as Dictionary <string, string>; if (dictionary == null) { dictionary = new Dictionary <string, string>(); object enumInstance = enumType.Assembly.CreateInstance(enumType.FullName); FieldInfo[] fieldInfos = enumType.GetFields(); foreach (FieldInfo fieldInfo in fieldInfos) { var descriptionAttributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), true); if (descriptionAttributes.Length > 0) { var key = ((int)fieldInfo.GetValue(enumInstance)).ToString(); var value = descriptionAttributes[0].Description; if (!dictionary.ContainsKey(key)) { dictionary.Add(key, value); } } } HttpContext.Current.Cache.Insert(cacheKey, dictionary); } var result = new OptimizedDictionary <string, string>(); foreach (var kvp in dictionary) { result.Add(kvp.Key, kvp.Value); } return(result); }