如何从通用获取数据注释?

时间:2019-01-29 08:15:47

标签: c# reflection data-annotations

我有一个已定义的通用类型和自定义数据注释。我想获取数据注释。

GetCustomAttributes不起作用,因为它是通用类型。

public List<ColumnFilter> SetColumnFilters()
{
    ColumnFilter columnFilter;
    List<ColumnFilter> columnFilters = new List<ColumnFilter>();        

    foreach (var prop in typeof(T).GetProperties())
    {
        columnFilter = new ColumnFilter();
        columnFilter.Field = prop.Name;
        Type type = prop.GetType();                    
        if (type is Enum)
        {
            columnFilter.FieldType = ColumnFilterType.DropDown;
        }
        else if (type is DateTime)
        {
            columnFilter.FieldType = ColumnFilterType.DatePicker;
        }
        else
        {
            columnFilter.FieldType = ColumnFilterType.Input;
        }
        columnFilters.Add(columnFilter);
     }
     return columnFilters;
}

0 个答案:

没有答案