I have the following extension method, and would like to make it more generic so I don’t have to implement it for every class in our domain.
public static IList<User> ToList(this DataTable table) { IList<User> users = new List<User>(); foreach (DataRow row in table.Rows) users.Add(User.FromDataRow(row)); return users; }
Is there any way to work around this frustrating limitation?
edit: the below paragraph is bollocks, but I’m keeping it so one of the answers makes sense to future readers:
User, as well as my other classes, implements IDataModel. IDataModel only requires 1 method, FromDataRow(DataRow row). Putting a where into the function prototype obviously doesn’t help.
When you only need one method, think Func… perhaps a
Func<DataRow, T>Then call
table.ToList<User>(User.FromDataRow)