Why does Invoke method fail with an argument exception?

Consider this sample code from a WinForms application:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            object[] parms = new object[1];
            parms[0] = "foo";

            DoSomething(parms);
        }

        public static string DoSomething(object[] parms)
        {
            Console.WriteLine("Something good happened");
            return null;
        }
    }

      

It works as expected, when you press button1 , it prints "Something good happened" to the console.

Now, consider this sample code, which is the same except that it calls DoSomething

using reflection:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            object[] parms = new object[1];
            parms[0] = "foo";

            System.Reflection.MethodInfo mi = typeof(Form1).GetMethod("DoSomething");
            mi.Invoke(null, parms);

        }

        public static string DoSomething(object[] parms)
        {
            Console.WriteLine("Something good happened");
            return null;
        }
    }

      

It prints System.ArgumentException

in string mi.Invoke(null, parms)

(object of type "System.String" cannot be converted to type "System.Object []".)

parms is clearly an array of objects, and the signature of the DoSomething method explicitly expects an array of objects. So why is invoke pulling the first object from the array and trying to pass it in?

Or is there something else going on, I don't understand?

+3


source to share


4 answers


MethodInfo.Invoke

expects an array of objects, where each object in the array of objects matches a method argument. The first argument in the array of objects is the first argument, the second object in the array is the second method, and so on.



Since you want the first argument of your method to be object[]

, you need to make sure that the first object in the array of objects that you pass in MethodInfo.Invoke

is an array of objects that represents the array that you want DoSomething

to use.

+7


source


object[] parms = new object[1];
parms[0] = "foo";

      

from:

public static string DoSomething(object[] parms)

      



what a problem; the first parameter is not string

- it is object[]

. object[]

which you pass in Invoke

represents each parameter in turn, so a object[]

length of 1 with a string will match static string DoSomething(string s)

but not match your method. Either change the signature or wrap the value. To be honest, I suggest changing the signature, this is the best idea here, but:

parms[0] = new object[] { "foo" };

      

will also work

+2


source


MethodInfo.Invoke

expects an array of objects as a parameter to pass multiple arguments to a function, each object in the array will be a different parameter.

Because your function also expects an array of objects, which you pass as an argument, not an array of objects, but a string.

You have to move this array to another array, so Invoke expands the first array and uses the internal array as the first parameter to call.

mi.Invoke(null, new object[]{ parms });

      

+1


source


parms is clearly an array of objects, and the DoSomething method signature explicitly expects an array of objects.

Yes, it expects an array of objects. But when you pass this:

object[] parms = new object[1];

      

You say these are arguments for DoSomething

, so parms [0] is the first argument DoSomething

, and if there parms

were more items in then parms [1] would be the second, and so on.

Obviously the first argument for is DoSomething

not string

(parms [0]), so you get this error:

It throws System.ArgumentException on mi.Invoke (null, parms) (An object of type "System.String" cannot be converted to type "System.Object []".)

+1


source







All Articles