Re: How to fill a generics List with reflection?
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Tue, 13 May 2008 13:03:14 -0700
On Tue, 13 May 2008 12:39:01 -0700, MrNobody <MrNobody@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
[...]
See how do I make a generic reference of that List? I think in Java you can
do something like List<?> so you dont need to explicitly specify type but I
cant figure out how to do it in C#.
I don't think you can do this in Java either. "List<?>" is sort of like "List<T>" in C# where T is a type parameter, but in either case it's a compile-time thing used to declare a generic. When you _use_ a generic, there has to be a real type there.
Do you really need your collection to be in a List<T>? Again, since the main benefit of generics is a compile-time thing, if you don't know the type at compile time, it's not clear why you want to use List<T>.
In this particular example, I would just use an ArrayList, which is an untyped collection that otherwise behaves similar to List<T>.
If you think that you really need a List<T> here, it would be helpful if you could elaborate on that, including presenting a code example that actually demonstrates that requirement. To do what you're asking literally I believe would at a minimum require using reflection again, instantiating the specific List<T> using Activator or similar, and then using reflection to invoke the appropriate Add() method.
But it seems to me that there's a strong likelihood that there's not really a literal need to use List<T>. If you can provide a better question, it's like you'll get a better answer, including an explanation of an alternative approach that doesn't require reflection at all for the collection itself.
Pete
.
- References:
- How to fill a generics List with reflection?
- From: MrNobody
- Re: How to fill a generics List with reflection?
- From: Peter Duniho
- Re: How to fill a generics List with reflection?
- From: MrNobody
- How to fill a generics List with reflection?
- Prev by Date: Re: Best Way to Test Against a Database
- Next by Date: Re: How to fill a generics List with reflection?
- Previous by thread: Re: How to fill a generics List with reflection?
- Next by thread: Re: How to fill a generics List with reflection?
- Index(es):
Relevant Pages
|