我已经得到了以下内容:
public class A {
public void theMethod(Object arg1) {
// do some stuff with a single argument
}
}
public class B {
public void reflectingMethod(Object arg) {
Method method = A.class.getMethod("theMethod", Object.class);
method.invoke(new A(), arg);
}
}
我如何修改它,使我可以做下面的事情?
public class A {
public void theMethod(Object... args) {
// do some stuff with a list of arguments
}
}
public class B {
public void reflectingMethod(Object... args) {
Method method = A.class.getMethod("theMethod", /* what goes here ? */);
method.invoke(new A(), args);
}
}
A.class.getMethod("theMethod", Object[].class);
Darthenius在评论中对最初问题的建议起作用了,一旦我开始思考如何做到这一点。
public class A {
public void theMethod(ArrayList<Object> args) { // do stuff
}
}
public class B {
public void reflectingMethod(ArrayList<Object> args) {
Method method;
try {
method = A.class.getMethod("theMethod", args.getClass());
method.invoke(new A(), args);
} catch (Exception e) {}
}
}