我有一个看起来像这样的方法;
public void update(List<Object> obj);
我希望能够使用java.lang.reflect.Method 来调用该方法。有谁知道该怎么做?
最佳答案
使用 Class
获取方法的类:
Class<?> clazz = theObject.getClass();
Method method = clazz.getMethod("update", List.class);
然后调用方法:
method.invoke(theObject, new ArrayList<Object>());
泛型在运行时被删除。它们是编译时的东西。因此,您可以使用 List.class
引用List<Object>
.
关于java - 将列表作为参数传递给 java.lang.reflect.Method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6136801/