获取枚举的Description Attribute:
 1  public static string GetEnumDescription(Enum enumSubitem)
 2         {
 3             if (enumSubitem == null)
 4                 return String.Empty;
 5 
 6             string strValue = enumSubitem.ToString();
 7 
 8             System.Reflection.FieldInfo fieldinfo = enumSubitem.GetType().GetField(strValue);
 9             if (fieldinfo == null)
10                 return "";
11             Object[] objs = fieldinfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
12             if (objs == null || objs.Length == 0)
13             {
14                 return strValue;
15             }
16             DescriptionAttribute da = (DescriptionAttribute)objs[0];
17             return da.Description;
18         }

针对下拉框等控件经常要绑定枚举类型的所有子项,并且显示的内容经常有空格,可自定义Attribute类型:

[自定义Attribute] 属性列表 易用于下拉框
  1 [AttributeUsage(AttributeTargets.Field, AllowMultiple=true)]
  2 public class CultureDescriptionAttribute : Attribute
  3 {
  4        
  5 private static readonly Dictionary<Type, 
  6     Dictionary<object,Dictionary<CultureInfo, CultureDescriptionAttribute>>> _cache = new Dictionary<Type,
  7     Dictionary<object, Dictionary<CultureInfo, CultureDescriptionAttribute>>>();
  8        
  9 private CultureInfo _culture;
 10       
 11 private string _description;
 12 
 13        
 14 public CultureDescriptionAttribute(string cultureName, string description)
 15     : this(CultureInfo.GetCultureInfo(cultureName), description)
 16 {
 17 }
 18 
 19        
 20 public CultureDescriptionAttribute(int cultureId, string description)
 21     : this(CultureInfo.GetCultureInfo(cultureId), description)
 22 {
 23 }
 24 
 25      
 26 private CultureDescriptionAttribute(CultureInfo culture, string description)
 27 {
 28     _culture = culture;
 29     _description = description;
 30 }
 31 
 32       
 33 public CultureInfo Culture
 34 {
 35     get
 36     {
 37         return _culture;
 38     }
 39 }
 40 
 41         
 42 public string Description
 43 {
 44     get
 45     {
 46         return _description;
 47     }
 48 }
 49  
 50 public static Dictionary<CultureInfo, CultureDescriptionAttribute> GetDisplayValues(
 51                                                               object value, Type enumType)
 52 {
 53     if (enumType == null || !enumType.IsEnum)
 54     {
 55         throw new NotSupportedException("enumType is not a Enum");
 56     }
 57     if (value == null || !Enum.IsDefined(enumType, value))
 58     {
 59         throw new ArgumentException("value is not defined in " + enumType.FullName);
 60     }
 61 
 62     return GetDisplayValuesImp(enumType,value);
 63 }
 64 
 65        
 66 public static Dictionary<int, string> GetDisplayValuesByCulture(Type enumType, CultureInfo culture)
 67 {
 68     Dictionary<int, string> enumValues=new Dictionary<int, string>();
 69     if (enumType == null || !enumType.IsEnum)
 70     {
 71         throw new NotSupportedException("enumType is not a Enum");
 72     }
 73     foreach (Enum item in Enum.GetValues(enumType))
 74     {
 75         FieldInfo enumField = enumType.GetField(item.ToString());
 76         enumValues.Add((int)enumField.GetValue(item), GetDisplayValuesImp(enumType,item)[culture].Description);
 77     }
 78     return enumValues;
 79 }
 80 
 81 public static Dictionary<int, string> GetDisplayValuesByCurrentCulture(Type enumType)
 82 {
 83     return GetDisplayValuesByCulture(enumType, CultureInfo.CurrentCulture);
 84 }
 85 
 86 
 87 public static string GetDisplayValue(Type enumType, object value,CultureInfo culture)
 88 {
 89     if (enumType == null || !enumType.IsEnum)
 90     {
 91         throw new NotSupportedException("enumType is not a Enum");
 92     }
 93     if (value == null || !Enum.IsDefined(enumType, value))
 94     {
 95         throw new ArgumentException("value is not defined in " + enumType.FullName);
 96     }
 97 
 98     if (culture == null)
 99         return value.ToString();
100 
101     Dictionary<CultureInfo, CultureDescriptionAttribute> disc = GetDisplayValuesImp(enumType,value);
102     if (disc.ContainsKey(culture))
103         return disc[culture].Description;
104     return value.ToString();
105 }
106 
107         
108 public  static Dictionary<CultureInfo, CultureDescriptionAttribute> GetDisplayValuesImp(
109                                                                         Type enumType,object value)
110 {
111     if (!_cache.ContainsKey(enumType))
112     {
113         Dictionary<object, Dictionary<CultureInfo, CultureDescriptionAttribute>> displayValues =
114         new Dictionary<object, Dictionary<CultureInfo, CultureDescriptionAttribute>>();
115         foreach (Enum item in Enum.GetValues(enumType))
116         {
117             Dictionary<CultureInfo, CultureDescriptionAttribute> descriptions =
118             new Dictionary<CultureInfo, CultureDescriptionAttribute>();
119             FieldInfo enumField = enumType.GetField(item.ToString());
120             if (enumField != null)
121             {
122                 foreach (CultureDescriptionAttribute desc in 
123                           enumField.GetCustomAttributes(typeof(CultureDescriptionAttribute), true))
124                 {
125                     descriptions.Add(desc.Culture, desc);
126                 }
127             }
128             displayValues.Add(item, descriptions);
129         }
130         _cache.Add(enumType, displayValues);
131     }
132 
133     return _cache[enumType][value];
134 }
135 }


 

posted on 2012-11-24 20:33  极简  阅读(394)  评论(0编辑  收藏  举报