先po一段在网上找的代码,获取C#枚举中的描述值的:
/// <summary> /// 获取描述信息 /// </summary> /// <param name="en">枚举</param> /// <returns></returns> public static string GetEnumDes(Enum en) { Type type = en.GetType(); MemberInfo[] memInfo = type.GetMember(en.ToString()); if (memInfo != null && memInfo.Length > 0) { object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false); if (attrs != null && attrs.Length > 0) return ((DescriptionAttribute)attrs[0]).Description; } return en.ToString(); }
时间: 2024-10-09 19:13:51