0

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??

2
  • 4
    My advice: write a small program that we can actually compile and run that demonstrates the problem. Then post that program. Otherwise it's just guessing; we don't know what is in arguments, objectType, testclass, or what the signature of setValues is, so we cannot diagnose the problem except psychically. Commented Jun 30, 2010 at 22:42
  • ... and as it's probably not an iterator block problem, psychic debugging is at a disadvantage ;) (blogs.msdn.com/b/ericlippert/archive/2007/09/05/…, blogs.msdn.com/b/ericlippert/archive/2007/09/06/…) Commented Jun 30, 2010 at 22:52

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.