This is a custom ORM thing we’re working on. It’s not awesome, but it’s what we’ve decided on doing.
Basically the base class Entity is extended by any number of other classes. These child classes must be annotated using attributes.
There is one particular type of attribute which is a collection. The attribute defines the sub-type of the collection.
eg.
public class Blah2 : Entity
{
...
}
public class Blah : Entity
{
[QRel(Type = typeof(Blah2)]
public IEnumerable<Blah2> blahs;
}
Loading these child objects is easily done, but they are loaded as the base type, Entity.
Via reflection, so at run time I have, in the ORM manager:
- The type marked in the attribute:
Type t - The field info of blahs:
FieldInfo f - The entity instance of type
Blah:i - A generic loaded list containing a set of
Blah2instances:List<Entity> l
I need to create a list lt using the derived type t (i.e. dynamically create and fill a List<Blah2>) and assign it to the field of type Blah:
var lt = ...
f.SetValue(i, lt);
Notice that because this is all done via reflection the types of the objects I have are: t -> Type, i -> Entity, f -> FieldInfo, l -> List<Entity>.
I would suggest that you create a helper class,
Helper<T>which does the allocation of the properly typed generic list, and fills the list with the items. You could cache an instance of the helper class in order to reduce the cost of reflection.To create an instance of the
Helper<T>, just do this:and then to allocate and fill the list of
Blah2based on the generic list: