You can't do this with a constraint. However, you can call the method and pass in a function to create a T from a MySqlDataReader:
public List<T> SelectAll<T>(Func<MySqlDataReader, T> projection)
where T : DatabaseObject
{
List<T> retVal = new List<T>();
String command = "Select * from " + GetType().Name;
MySqlCommand cmd = DatabaseRunner.GetCommand(command);
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
retValue.Add(projection(reader));
}
return retVal;
}
(You may well want to use DbDataReader instead of MySqlDataReader to avoid being so specific.)
Another alternative is to give DatabaseObject something like an Initialize(DbDataReader reader) method, then you could use:
public List<T> SelectAll<T>() where T : DatabaseObject, new()
{
List<T> retVal = new List<T>();
String command = "Select * from " + GetType().Name;
MySqlCommand cmd = DatabaseRunner.GetCommand(command);
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
T value = new T();
value.Initialize(reader);
retValue.Add(value);
}
return retVal;
}
As a side issue, you're not disposing of anything - you should really be using using statements to dispose of the reader and the command automatically.