I'd like to populate a list of objects from a DataTable without explicitly assigning data elements column-by-column and without using an ORM. With Dapper (a small ORM) one can pass a list of POCO (ordinary objects) and have it auto-match "cells" from a query based on the "shape" of that POCO. I'd like to do something similar using a DataTable object. Is there a way to mirror that functionality without using Dapper? I suppose reflection can be used, but getting reflection to work reliably often requires "rocket science", especially in terms of debugging. Performance is a relatively minor concern.
public class Employee
{
public string lastName { get; set; }
public string firstAndMidName { get; set; }
public int employeeNumber { get; set; }
public int salary { get; set; }
}
// ...
public void runSample()
{
List<Employee> employeeList = new List<Employee>();
DataTable myDat = queryRDBMS("select * from Employees"); // typical query API (simplified)
employeeList = convertDataTableToList(myDat, modelClass: Employee); // the missing part
}
(Updated)