2011-05-09 54 views
1

我試圖使用反映,在這裏,我需要在arguments.but我不是爲了實現這一目標通過通用類「clazz中」調用方法..需要通過泛型類的反射get方法

在這裏例如,我嘗試使用類,但這是行不通的

 public void log( Class<?> clazz,Throwable throwable, String pattern) { 
    Method method= CommonsLogger.class.getMethod("info", Class<T>,String.class,Throwable.class); 
    //try catch removed 
    } 

回答

0

你不能這樣做。這應該工作,

CommonsLogger.class.getMethod("info", String.class, Throwable.class) 
+0

所以這意味着我可以通過getmethod中的泛型類..這是正確的 – Vish 2011-05-09 07:56:14

+0

是的,確切地說。請閱讀http://download.oracle.com/javase/tutorial/java/generics/erasure.html – 2011-05-09 07:57:53