• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reflectively Instantiating a Generic List  RSS feed

 
Rob Roy
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgive me if this has been asked in another form (I did search and didn't find much), but I'd like to instantiate a List, with generic type information reflectively. For example, here's some code that I'd like to make work (pseudo-java):



I've been looking around for a way to instantiate the list, and haven't found a whole lot. This is for a factory (and "someClassname" will be populated from an external source when complete), and I've not quite solved the problem of the type safety issue when actually using the list, but I figured I'd take it one step at a time.

Any suggestions are welcomed!

Thanks,
Rob
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generics checking is done at compile time. All types are non-existent at runtime. Reflection is a runtime operation. So... you can't. The best you can do is instantiate a list of objects.

On the other hand, you can do runtime type checking. Take a look at the Collections class -- particularly the checked collections.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!