Passing List as Parameter to java.lang.reflect.Method
I have a meth开发者_运维技巧od which looks like this;
public void update(List<Object> obj);
I want to be able to use java.lang.reflect.Method to invoke the method. Do anyone know how to go about doing this?
Use the Class
class to get the method:
Class<?> clazz = theObject.getClass();
Method method = clazz.getMethod("update", List.class);
and then invoke the method:
method.invoke(theObject, new ArrayList<Object>());
Generics are erased at runtime. They're a compile-time thing. You may thus use List.class
to refer to a List<Object>
.
yes, you can do
myObject.getClass()
.getMethod("update", List.class)
.invoke(myObject, myListOfObject);
精彩评论