How to call MethodInvoke - reflection

后端 未结 1 1152
猫巷女王i
猫巷女王i 2020-12-12 07:48

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

         


        
相关标签:
1条回答
  • 2020-12-12 08:25

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

    0 讨论(0)
提交回复
热议问题