tags:

views:

41

answers:

2

Hey all, I have a problem with this:

objectType.GetMethod("setValues").Invoke(testClass, arguments);

arguments is an array of objects, can any member of it be an array of any type like this int[]??? I'm asking this because I have an exception when passing arguments with an int[] array as a member in it, this is the exception:

System.ArgumentException: Object of type 'System.Object[]' cannot be converted to type 'System.Int32[]'.

Any suggestions??

+1  A: 

Yes, you should be able to pass an array of integers as one of the parameters to a reflection call. From the error it looks like what you think is an array of integers is actually an array of objects though.

R0MANARMY
A: 

My psychic debugging suggests that the signature of setValues looks like this:

public void setValues(int[] vals);

And you're passing an int[] to Invoke, like so:

int[] arguments = new int[] { 1, 2, 3 };
objectType.GetMethod("setValues").Invoke(testClass, arguments);

This doesn't work, because the object[] that you pass to Invoke is flattened to create the actual argument list. In other words, your invocation is equivalent to calling setValues(1, 2, 3), which doesn't work because setValues wants a single int[] parameter, not a series of int parameters. You need to do the following:

int[] values = new int[] { 1, 2, 3 };
object[] arguments = new object[] { values };
objectType.GetMethod("setValues").Invoke(testClass, arguments);
JSBangs