Jim Feng

Jim Feng

  • NA
  • 29
  • 0

Declare a new object instance from an exist object instance

Oct 25 2017 12:10 PM
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)