我想在運行時使用反射來獲取集合的通用類型。在運行時指定通用集合類型參數(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