有些属性需要参数,有些只写,要过滤掉:
PropertyInfo[] entityProperties = entityType
.GetProperties()
.Where(p => p.CanRead && p.GetIndexParameters().Length == 0)
.ToArray();
PropertyInfo[] entityProperties = entityType
.GetProperties()
.Where(p => p.CanRead && p.GetIndexParameters().Length == 0)
.ToArray();
参考
http://msdn.microsoft.com/zh-cn/library/b05d59ty.aspx
demo:
http://www.cnblogs.com/dyfzwj/archive/2011/04/16/2017916.html
public DataTable FillDataTable(List<T> modelList)
{
if (modelList == null || modelList.Count == 0)
{
return null;
}
DataTable dt = CreateData(modelList[0]); foreach(T model in modelList)
{
DataRow dataRow = dt.NewRow();
foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
{
dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null);
}
dt.Rows.Add(dataRow);
}
return dt;
}