I have all kind of classes that represents entities, for example:
public class Person
{
public int PersonID { get; set; }
public string PersonName { get; set; }
public bool IsSystemManager { get; set; }
public bool IsSystemAdmin { get; set; }
public Person() { }
public Person(DataRow row)
{
PersonID = (int)row["person_id"];
PersonName = row["person_name"] as string;
IsSystemManager = (bool)row["is_manager"];
IsSystemAdmin = (bool)row["is_admin"];
}
}
And I want to have an extension to DataTable
that turns it into a list of a class object, like this:
public static List<T> ToObjectList<T>(this DataTable table) where T : new(DataRow dr) //this is a compilation error
{
List<T> lst = new List<T>();
foreach (DataRow row in table.Rows)
lst.Add(new T(row));
return lst;
}
But I can't have the constraint for contstructor with DataRow
as a parameter.
Is there any way to have an extension method like this?