如何调用MethodInvoke-反射

发布于 2024-12-17 07:06:26 字数 251 浏览 1 评论 0原文

如果我有一个采用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜扑 2024-12-24 07:06:27

Method.invoke 采用两个参数。第一个是目标,obj,这是正确的。第二个是一个数组,表示您尝试调用的实际方法的零个或多个参数(许多方法有多个参数)。您的代码应更改为:

method.invoke(obj, new Object[] { anArray });

这样,您所说的“使用一个参数调用此方法,并且该参数是一个数组。”这与“使用 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:

method.invoke(obj, new Object[] { anArray });

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文