There are two big problems:
- You cannot specify a constructor constraint that accepts a parameter
- Your method is currently not general - it should be
PopulateCollection<T>instead PopulateCollection.
T : BusinessBase, , , ( ) BusinessBase:
public abstract void PopulateFrom(DataRow dr);
T.
:
protected List<T> PopulateCollection(DataTable dt)
where T: BusinessBase, new()
{
List<T> lst = new List<T>();
foreach (DataRow dr in dt.Rows)
{
T t = new T();
t.PopulateFrom(dr);
lst.Add(t);
}
return lst;
}
.NET 3.5, DataTableExtensions:
protected List<T> PopulateCollection<T>(DataTable dt)
where T: BusinessBase, new()
{
return dt.AsEnumerable().Select(dr =>
{
T t = new T();
t.PopulateFrom(dr);
}.ToList();
}
( , .NET 3.5) :
static List<T> ToList<T>(this DataTable dt, Func<DataRow dr, T> selector)
where T: BusinessBase
{
return dt.AsEnumerable().Select(selector).ToList();
}
:
table.ToList(row => new Whatever(row));
, , DataRow. ( , ), , , "factory".