/// <summary> /// 获取枚举描述信息 /// </summary> /// <param name="enumType"></param> /// <returns></returns> public static string GetDescription(Type enumType, Enum enumValue) { Type type = enumType; string key = type.ToString() + "." + enumValue.ToString(); if (enumDescriptions.ContainsKey(key)) { return(enumDescriptions[key]); } FieldInfo f = type.GetField(enumValue.ToString()); string result = string.Empty; if (f != null) { EnumAttribute attr = Attribute.GetCustomAttribute(f, typeof(EnumAttribute)) as EnumAttribute; if (attr != null) { result = attr.Description; } lock (locker) { enumDescriptions.Add(key, result); } return(result); } return(result); }
/// <summary> /// 返回枚举项的描述信息。 /// </summary> /// <param name="value">要获取描述信息的枚举项。</param> /// <returns>枚举想的描述信息。</returns> private static string GetDescription(Enum value) { Type enumType = value.GetType(); // 获取枚举常数名称。 string name = Enum.GetName(enumType, value); if (name != null) { // 获取枚举字段。 FieldInfo fieldInfo = enumType.GetField(name); if (fieldInfo != null) { // 获取描述的属性。 EnumAttribute attr = Attribute.GetCustomAttribute(fieldInfo, typeof(EnumAttribute)) as EnumAttribute; if (attr != null) { return(attr.Description); } } } return(null); }