Re: How to fill a generics List with reflection?

Tech-Archive recommends: Fix windows errors by optimizing your registry



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
.



Relevant Pages

  • Re: foreach loop and generic class
    ... if your list is supposed to contain DataRowView ... So far I don't really see anything that using generics is going to help ... interested in properties that existed in some base class, ... Then instead of using reflection you could simply use the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to assign a reference to a class member to another class member?
    ... then Undo() can copy back. ... Involves boxing and reflection, but little maintenance. ... correctly and using generics) avoid boxing and reflection, ... public void Commit() { ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: foreach loop and generic class
    ... what relationship does the list of properties from the type have to the columns in the DataRowView object that's supposed to be in the list? ... If you really need to enumerate the properties of a class and fill some data table's columns with the values of those properties, you can do that without generics. ... From an instance you can get its type, from the type you can get the properties, and then of course you can use reflection to get the values of those properties to fill each row in your data table. ... The only way I could see generics being useful is if you were only interested in properties that existed in some base class, and you wanted to deal with Listwhere you could constrain T to inherit that base class. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Generic object manipulation
    ... the problem is that even using reflection really didn't get me ... It would be silly to put all these functions inside the DataArray ... internal Type GetTypeA() ... type parameters are when you call Process, then generics aren't really ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Generic casting
    ... That is, with reflection, you are going to have a run-time check against the type, while generics are going to be evaluated by the compiler at compile time. ... something tells me that you are using reflection because you don't have a reference to the actual type that you want to cast to. ...
    (microsoft.public.dotnet.languages.csharp)