如何调用MethodInvoke-反射
如果我有一个采用 int[]
作为参数的方法,并且我希望对此调用 method.invoke
那么我需要执行以下操作
Object[] anArray = {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};
method.invoke(obj, anArray);
吗?因为我似乎遇到了错误?
问候
If i have a method which takes a int[]
as a parameter and i wish to call method.invoke
on this then do i need to do the following
Object[] anArray = {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};
method.invoke(obj, anArray);
Is it as simple as that as i seem to be getting errors?
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Method.invoke 采用两个参数。第一个是目标,
obj
,这是正确的。第二个是一个数组,表示您尝试调用的实际方法的零个或多个参数(许多方法有多个参数)。您的代码应更改为:这样,您所说的“使用一个参数调用此方法,并且该参数是一个数组。”这与“使用 10 个参数调用此方法”(数组中的每个元素一个)不同)。
Method.invoke
takes two arguments. The first is the target,obj
, which is correct. The second is an array representing zero or more arguments for the actual method you are trying to invoke (many methods have more than one parameter). Your code should change to:This way, you're saying "invoke this method with one argument, and that argument is an array. This is different from saying, "invoke this method with 10 arguments" (one for each element in your array).