net core中自定义特性以及获取自定义特性的方法
1、新建FieldAliasNameAttribute类,继承ValidationAttribute
////// 自定义表格中的字段特性 /// public class FieldAliasNameAttribute : ValidationAttribute { /// /// 显示的名称 /// public string Name { get; set; } public FieldAliasNameAttribute(string Name) { this.Name = Name; } }
2、在实体类中调用FieldAliasName特性
////// 活动名称 /// [MaxLength(30)] [FieldAliasName("活动名称")] public string HUODONG_MC { get; set; }
调用该特性的方法如下
1、新建方法
////// 获取列特性 /// /// /// /// public static FieldAliasNameAttribute GetTableColumnAttribute(string propertyName, Type t) { if (t == null) { return null; } FieldAliasNameAttribute m = null; //Type t = typeof(T); PropertyInfo[] arryProperty = t.GetProperties(); if (arryProperty != null) { foreach (PropertyInfo p in arryProperty) { if (p.Name == propertyName) { m = p.GetCustomAttribute (); } } } return m; }
2、方法调用
FieldAliasNameAttribute colId = GetTableColumnAttribute("HUODONG_MC", 实体类); var aliasName=colId.Name