By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,110 Members | 1,535 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,110 IT Pros & Developers. It's quick & easy.

Dynamic Types in a List<>

P: n/a
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.
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Eric <Er**@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Eric" <Er**@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
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.

Nov 17 '05 #3

P: n/a
since you are using reflection anyway, I don't see how making a strongly
typed list is gonna help you.

"Eric" wrote:
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.

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.