I would like to be able to pass a Tuple to the following method, however I can't since I have to instantiate it.
public List<T> Select<T>(string sql, DbCommand command) where T : new()
{
DbDataReader dataReader = null;
List<T> ls = new List<T>();
Type tType = typeof(T);
T t;
PropertyInfo prop = null;
string[] propertiesNames = GetPropertiesNamesFromSQL(sql);
try
{
dataReader = command.ExecuteReader();
if (dataReader == null || !dataReader.HasRows) return ls;
while (dataReader.Read())
{
t = new T();
for (int i = 0; i < dataReader.FieldCount; i++)
{
prop = tType.GetProperty(propertiesNames[i]);
prop.SetValue(t, dataReader.GetValue(i));
}
ls.Add(t);
}
}
catch (Exception){ }
finally
{
if (dataReader != null) dataReader.Close();
}
return ls;
}
As I said in the comments, I'm trying to build a bare bone ORM, I am only interested in the mapping to objects part. It works as is, however it would be practical sometimes for quick queries to be able to pass a Tuple instead of a full class.
Is there any solution ?