is there a way, maybe using reflection, to use a generic class passing
the
type parameter dynamicly (not kwnowing the exact type at compile
time)?
I tried the next example, but doesn't work:
public abstract class Vehicle
{ ... }
public class Aircraft : Vehicle
{ ... }
public class SimpleList<ATypewhere AType:Vehicle
{
...
}
call:
Aircraft Boeing747 = new Aircraft();
SimpleList<typeof(Aircraft)TheList = new
SimpleList<Boeing747.GetType()>();
...
Thanks,
The example that you presented doesn't make much sense (since you obviously
know that you're dealing with an Aircraft). However, you can use reflection
to create a generic type using a System.Type as the generic type argument:
Aircraft boeing747 = new Aircraft();
Type simpleListType = typeof(SimpleList<>);
Type boeingListType = simpleListType.MakeGenericType(boeing747.GetType() );
object theList = Activator.CreateInstance(boeingListType);
However, if you have to write code like that, you probably aren't using generics
in a way that makes sense. You might want to rethink your design.
Best Regards,
Dustin Campbell
Developer Express Inc.