/// <summary>
/// 备注特性
/// </summary>
public class RemarkAttribute : Attribute
{
/// <summary>
/// 备注
/// </summary>
public string Remark { get; set; }
public RemarkAttribute(string remark)
{
this.Remark = remark;
}
}
logs_code_copy" style="font-size: 16px;">
/// <summary>
/// 枚举扩展类
/// </summary>
public static class EnumExtension
{
/// <summary>
/// 获取枚举的备注信息
/// </summary>
/// <param name="em"></param>
/// <returns></returns>
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();