初始化提交
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
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<PropertyInfo> GetPropertiesWithAnnotation<T>(Type type) where T : Attribute
|
||||
{
|
||||
var properties = type.GetProperties().Where(p => p.GetCustomAttributes(typeof(T), true).Length > 0).ToList();
|
||||
return properties;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前类的DataGridViewColumn
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static List<DataGridViewColumn> GetDataGridViewColumns(this object obj)
|
||||
{
|
||||
Type typeObj = obj.GetType();
|
||||
var properties = AnnotationUtils.GetPropertiesWithAnnotation<DataGridViewColumn>(typeObj);
|
||||
|
||||
List<DataGridViewColumn> list = new List<DataGridViewColumn>();
|
||||
foreach (var property in properties)
|
||||
{
|
||||
DataGridViewColumn attribute = property.GetCustomAttribute<DataGridViewColumn>();
|
||||
//数据源名称
|
||||
attribute.DataPropertyName = property.Name;
|
||||
list.Add(attribute);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user