• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generic Collection Type  RSS feed

 
Dave Radtke
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to create a function that can determine the Component Type of any generic collection. I would have though this would be an easy question, but after many hours of searching I haven't found anything.

I have found this works, but only if the collection contains a value, which I cannot rely on.



Thanks
Dave
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because of erasure that information is no longer available at runtime.
That sort of question comes up several times a year; if you search you will find suggestions, but there is no straightforward way to retrieve "<T>" and workout what "T" stands for.
 
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
I have found this works, but only if the collection contains a value, which I cannot rely on.


Also, it may not be reliable... for example, if someone calls your method with a Collection<Animal> object, that contains Dogs, Cats, Horses, Cows, etc.... With your solution, you can mistakenly get the first element, which may be a Cow, and assume that it is a Collection<Cow> object.

Henry
 
Dave Radtke
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Because of erasure that information is no longer available at runtime.
That sort of question comes up several times a year; if you search you will find suggestions, but there is no straightforward way to retrieve "<T>" and workout what "T" stands for.


Well, I feel better that at least I'm not just missing the obvious.

I have been searching, and the closes I have found was this Getting-generic-type-Collection but that sample appears to only work if you have the collection as a declared field within another object. I already have a method to workout it out from a PropertyDescriptor. But in this situation all I have is the collection itself.


What search terms should I be using?



 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!