我想在运行时使用反射来获取集合的通用类型。在运行时指定通用集合类型参数(Java反射)
代码(JAVA):
Field collectionObject = object.getClass().getDeclaredField(
collectionField.getName());
//here I compare to see if a collection
if (Collection.class.isAssignableFrom(collectionObject.getType())) {
// here I have to use the generic type of the collection
// to see if it's from a specific type - in this case Persistable
if (Persistable.class.isAssignableFrom(GENERIC_TYPE_COLLECTION.class)) {
}
}
是否有在运行时获得泛型类型的集合在Java中的一种方式?在我的情况下,我需要该集合的泛型类型的.class。
在此先感谢!
我正面临着类似的问题,这终于为我解决了这个问题:http://stackoverflow.com/a/183232/639119 – Zds 2012-10-04 18:43:22