using System;using System.Reflection;using System.Collections;using System.Collections.Generic;namespace AttributeMeta{ [AttributeUsage(AttributeTargets.Field | AttributeTargets.Enum)] public class EnumDescription : Attribute { private FieldInfo fieldInfo; public EnumDescription(string text, int rank) { this.Text = text; this.Rank = rank; } ////// 描述枚举值,默认排序为5 /// /// 描述内容 public EnumDescription(string text) : this(text, 5) { } public string Text { get; set; } public int Rank { get; set; } public int Value { get { return (int)fieldInfo.GetValue(null); } } public string FieldName { get { return fieldInfo.Name; } } #region 对枚举描述属性的解释相关函数 ////// 排序类型 /// public enum SortType { //////按枚举顺序默认排序 /// Default, ////// 按描述值排序 /// DisplayText, ////// 按排序熵 /// Rank } private static Hashtable cachedEnum = new Hashtable(); ////// 得到对枚举的描述文本 /// /// 枚举类型 ///public static string GetEnumText(Type enumType) { EnumDescription[] eds = (EnumDescription[])enumType.GetCustomAttributes(typeof(EnumDescription), false); if (eds.Length != 1) return string.Empty; return eds[0].Text; } /// /// 获得指定枚举类型中,指定值的描述文本。 /// /// 枚举值,不要作任何类型转换 ///描述字符串 public static string GetFieldText(object enumValue) { EnumDescription[] descriptions = GetFieldTexts(enumValue.GetType(), SortType.Default); foreach (EnumDescription ed in descriptions) { if (ed.fieldInfo.Name == enumValue.ToString()) return ed.Text; } return string.Empty; } ////// 得到枚举类型定义的所有文本,按定义的顺序返回 /// ////// 枚举类型 /// 所有定义的文本 public static EnumDescription[] GetFieldTexts(Type enumType) { return GetFieldTexts(enumType, SortType.Default); } ////// 得到枚举类型定义的所有文本 /// ////// 枚举类型 /// 指定排序类型 /// 所有定义的文本 public static EnumDescription[] GetFieldTexts(Type enumType, SortType sortType) { EnumDescription[] descriptions = null; //缓存中没有找到,通过反射获得字段的描述信息 if (cachedEnum.Contains(enumType.FullName) == false) { FieldInfo[] fields = enumType.GetFields(); ArrayList edAL = new ArrayList(); foreach (FieldInfo fi in fields) { object[] eds = fi.GetCustomAttributes(typeof(EnumDescription), false); if (eds.Length != 1) continue; ((EnumDescription)eds[0]).fieldInfo = fi; edAL.Add(eds[0]); } cachedEnum.Add(enumType.FullName, (EnumDescription[])edAL.ToArray(typeof(EnumDescription))); } descriptions = (EnumDescription[])cachedEnum[enumType.FullName]; if (descriptions.Length <= 0) throw new NotSupportedException("枚举类型[" + enumType.Name + "]未定义属性EnumValueDescription"); //按指定的属性冒泡排序 for (int m = 0; m < descriptions.Length; m++) { //默认就不排序了 if (sortType == SortType.Default) break; for (int n = m; n < descriptions.Length; n++) { EnumDescription temp; bool swap = false; switch (sortType) { case SortType.Default: break; case SortType.DisplayText: if (string.Compare(descriptions[m].Text, descriptions[n].Text) > 0) swap = true; break; case SortType.Rank: if (descriptions[m].Rank > descriptions[n].Rank) swap = true; break; } if (swap) { temp = descriptions[m]; descriptions[m] = descriptions[n]; descriptions[n] = temp; } } } return descriptions; } ////// 获得枚举类型数据的列表 /// /// 枚举类型 ///Meta 列表 public static List GetMeta(Type enumType) { List list = new List (); FieldInfo[] fields = enumType.GetFields(); foreach (FieldInfo fi in fields) { object[] eds = fi.GetCustomAttributes(typeof(EnumDescription), false); if (eds.Length != 1) continue; ((EnumDescription)eds[0]).fieldInfo = fi; var item = eds[0] as EnumDescription; Meta meta = new Meta { MetaName = item.FieldName, MetaRank = item.Rank, MetaText = item.Text, MetaValue = item.Value }; list.Add(meta); } return list; } ////// 返回枚举类型数据的哈希表 /// /// 枚举类型 ///Hashtable public static Hashtable GetMetaTable(Type enumType) { Hashtable table = new Hashtable(); FieldInfo[] fields = enumType.GetFields(); foreach (FieldInfo fi in fields) { object[] eds = fi.GetCustomAttributes(typeof(EnumDescription), false); if (eds.Length != 1) continue; ((EnumDescription)eds[0]).fieldInfo = fi; var item = eds[0] as EnumDescription; table.Add(item.Value, item.Text); } return table; } ////// 根据枚举值获得枚举文本 /// /// 枚举类型 /// 枚举值 ///Text public static object GetMetaValue(Type enumType, int key) { object value = null; Hashtable table = GetMetaTable(enumType); if (table.Count > 0) { value = table[key]; } return value; } ////// 根据枚举文本获得枚举值 /// /// 枚举类型 /// 枚举文本 ///Value public static object GetMetaKey(Type enumType, string value) { object key = null; Hashtable table = GetMetaTable(enumType); if (table.Count > 0) { foreach (DictionaryEntry de in table) { if (de.Value.Equals(value)) { key = de.Key; } } } return key; } #endregion } public class Meta { public string MetaName { get; set; } public int MetaValue { get; set; } public string MetaText { get; set; } public int MetaRank { get; set; } }}