PropertyInfo[] props = typeof(T).GetProperties();
Dictionary<string, ColumnInfo> _colsDict = new Dictionary<string, ColumnInfo>();
foreach (PropertyInfo prop in props)
{
object[] attrs = prop.GetCustomAttributes(true);
foreach (object attr in attrs)
{
ColumnInfo colInfoAttr = attr as ColumnInfo;
if (colInfoAttr != null)
{
string propName = prop.Name;
_colsDict.Add(propName, colInfoAttr);
}
}
}
答案 0 :(得分:1)
如果您知道基类类型,则可以执行以下操作:
public static Dictionary<string, object> GetProperties<Derived, Base>()
{
var onlyInterestedInTypes = new[] { typeof(Derived).Name, typeof(Base).Name };
return Assembly
.GetAssembly(typeof(Derived))
.GetTypes()
.Where(x => onlyInterestedInTypes.Contains(x.Name))
.OrderBy(x => x.IsSubclassOf(typeof(Base)))
.SelectMany(x => x.GetProperties())
.GroupBy(x => x.Name)
.Select(x => x.First())
.ToDictionary(x => x.Name, x => (object)x.Name);
}
.OrderBy(x => x.IsSubclassOf(typeof(Base)))
对您来说很重要,它将对属性进行排序。