Dynamic Types in a List<>

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a string representation of an object. I create an object of that type
through reflection. I would like to create a List<> of those objects. I
obviously can't do
List<myObject.GetType()>

or

Type t = object.GetType();
List<t>;

Any suggestions.
 
Eric said:
I have a string representation of an object. I create an object of that type
through reflection. I would like to create a List<> of those objects. I
obviously can't do
List<myObject.GetType()>

or

Type t = object.GetType();
List<t>;

Any suggestions.

If you look at the docs for Type.IsGenericType in MSDN, there's a link
at the bottom for how to use generics with reflection. I think that
should help you out.
 
Eric,

If all of the objects derive from a common base class, or they implement
the same interface, you can create a List<BaseClass> or List<Interface>
(assuming BaseClass or Interface are the names of your base class or
interface, respecitvely).

Otherwise, you would have to create a List<object> since object is the
root of everything.

Hope this helps.
 
since you are using reflection anyway, I don't see how making a strongly
typed list is gonna help you.
 
Back
Top