【发布时间】:2012-10-05 02:43:22
【问题描述】:
我想在运行时使用反射获取集合的泛型类型。
代码(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。
提前致谢!
【问题讨论】:
-
我遇到了类似的问题,这就是最终为我解决的问题:stackoverflow.com/a/183232/639119
标签: java reflection