Trying to customize my petapoco experience, I have following situation:
class Animal
{
// common properties
}
class Dog:Animal
{
//particular properties including custom attributes for properly working with DB
}
class Cat:Animal
{
}
Normally, I use
List<Dog> ret = db.Query<Dog>("select * from dogs").ToList();
I created a list (Category) which keep tracks between tables in database and objects actually implemented in my code (eg. class dog)
So I obtain the type at runtime from Category list:
Type t = ((Category)Categories.Current).ObjectType;
In my debugger I can see the Dog
type for t. But I don't know how to convert the runtime obtained type into something usable for
List<t> ret = db.Query<t>("select * from ...").ToList();
Thanks in advance,
PS. I don't want to use base Animal for query List since the result list will populate a GridView
and derived classes (eg. Dog) contains specific attributes for displaying, filter, etc.
I think the best you can do is to have something like this:
Note that you cannot use
List<Animal>
as return type, as a list of a derived type is not assignment compatible to a list of a base type.IList
is the greatest common denominator here. But there is a chance that the GridView actually sees the realList<T>
assigned to its data source. (Usually data binding sources are typed asobject
anyway.Fetch<T>()
is likeQuery<T>
but returns aList<T>
instead of aIEnumerable<T>
.