将列表作为参数传递给java.lang.reflect.Method

问题描述

|| 我有一个看起来像这样的方法
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>
。     ,是的,你可以
myObject.getClass()
    .getMethod(\"update\",List.class)
    .invoke(myObject,myListOfObject);