Biscuit128
Biscuit128

Reputation: 5408

How to call MethodInvoke - reflection

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

Upvotes: 1

Views: 931

Answers (1)

Kirk Woll
Kirk Woll

Reputation: 77606

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

Upvotes: 6

Related Questions