I am trying to iterate through the generic type object list, i am able to get the properties of the object however unable to get the values from properties of each instance of the object. Here's how my code looks like: I want create a function that will convert any list passed to it and convert it into DataTable.
--DataObject
public class StudentDo
{
public int Id {get;set}
public string Name {get;set}
}
--Generic Data Access Object
public DataTable ConvertListToDataTable(List<T> list, string tableName = "")
{
var type = typeof(T);
var properties = type.GetProperties().ToList();
DataTable dt = new DataTable(tableName);
properties.ForEach(x =>
{
dt.Columns.Add(x.Name);
});
// i don't know how shall i pull data from each instance of List<T>.
return dt;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…