////// 备注特性 /// public class RemarkAttribute : Attribute { ////// 备注 /// public string Remark { get; set; } public RemarkAttribute(string remark) { this.Remark = remark; } }
////// 枚举扩展类 /// public static class EnumExtension { ////// 获取枚举的备注信息 /// /// ///public static string GetRemark(this Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); if (fi == null) { return value.ToString(); } object[] attributes = fi.GetCustomAttributes(typeof(RemarkAttribute), false); if (attributes.Length > 0) { return ((RemarkAttribute)attributes[0]).Remark; } else { return value.ToString(); } } public static string GetEnumDescription(this Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes.Length > 0) { return attributes[0].Description; } else { return value.ToString(); } } }
var aaa = UserType.Type1.GetRemark(); var aab = UserType.Type2.GetEnumDescription();