/// <summary> /// 获取枚举值定义的属性 /// </summary> /// <param name="value"></param> /// <returns></returns> private static EnumAttribute GetAttribute(Enum value) { if (enumAttr.ContainsKey(value)) { EnumAttribute ea = enumAttr[value]; return(ea); } else { FieldInfo field = value.GetType().GetField(value.ToString()); if (field == null) { return(null); } EnumAttribute ea = null; object[] attributes = field.GetCustomAttributes(typeof(EnumAttribute), true); if (attributes != null && attributes.Length > 0) { ea = (EnumAttribute)attributes[0]; } enumAttr[value] = ea; return(ea); } }
/// <summary> /// 获取枚举值的名称,该名称由EnumAttribute定义 /// </summary> /// <param name="value">枚举值</param> /// <returns>枚举值对应的名称</returns> public static string GetDescription(Enum value) { EnumAttribute ea = GetAttribute(value); return(ea != null ? ea.Description : ""); }
/// <summary> /// 获取枚举值的名称,该名称由EnumAttribute定义 /// </summary> /// <param name="value">枚举值</param> /// <returns>枚举值对应的名称</returns> public static string GetName(Enum value) { EnumAttribute ea = GetAttribute(value); return(ea != null ? ea.Name : ""); }