当方法具有变量 arglist 时的 Java 反射
我有一些类似于以下内容的内容:
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);
}
}
I've got something along the lines of the following:
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);
}
}
How do I modify that so that I can do the following instead?
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一旦我开始思考如何做到这一点,达尼纽斯在原始问题评论中的建议就奏效了。
Darthenius's suggestion in the comments for the original question worked, once I wrapped my head around how to do it.