using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace JinYuan.Models.HelperAttribute { public static class AnnotationUtils { public static List GetPropertiesWithAnnotation(Type type) where T : Attribute { var properties = type.GetProperties().Where(p => p.GetCustomAttributes(typeof(T), true).Length > 0).ToList(); return properties; } /// /// 获取当前类的DataGridViewColumn /// /// /// public static List GetDataGridViewColumns(this object obj) { Type typeObj = obj.GetType(); var properties = AnnotationUtils.GetPropertiesWithAnnotation(typeObj); List list = new List(); foreach (var property in properties) { DataGridViewColumn attribute = property.GetCustomAttribute(); //数据源名称 attribute.DataPropertyName = property.Name; list.Add(attribute); } return list; } } }