2
Reply

Declare a new object instance from an exist object instance

Jim Feng

Jim Feng

Oct 25 2017 12:10 PM
147
I try to code a common method to fill in an class object list with datatable as following:
 
public static List FillDataTable(object obj, DataTable dt)
{
List objList = new List();
object o = new object(); //how to declare this instance based on the object obj???
foreach (DataRow dr in dt.Rows)
{
//code to fill datarow to the object o
objList.Add(o);
}
return objList;
}
 
Call this method as followiing:
 
List lstObj = new List();
lstObj = FillDataTable(((object)new myClass()), dt).OfType().ToList();

Answers (2)