0

This is my code and it works:

Value[] params2 = {val};
Class<?> clazz = Class.forName("package." + className);
Method[] thisMethod = clazz.getMethods();
thisMethod[0].invoke(clazz.newInstance(), new Object[]{params2});

However when I try to send list of 'val' objects as a parameter,

List<Value>[] params2 = {valList};

I get: 'Cannot create a generic array of List error'

How can I pass list of val objects as a paramter?


You need to pass an array of arguments to invoke a method reflectively, not a List of arguments. If you have a List, use its toArray() method.

4

2 回答 2

0

List<Value>[] means array where members of this array are lists. Ie. {new ArrayList<Value>()} is the instance. If your methods takes as an argument a list, this is the way. Otherwise you should convert the list of params to array, because that is how the Java reflection API was designed.

于 2012-10-04T16:15:31.670 回答
0

您需要传递参数数组来反射调用方法,而不是List参数。如果你有一个List,使用它的toArray()方法。

于 2012-10-04T15:45:33.557 回答