Using Method.Invoke on a method that accepts an array parameter...

  • Thread starter Thread starter nfedin
  • Start date Start date
N

nfedin

I am writing a testing application that uses reflection to open
assemblies and call methods.

I have a function that looks like this...

public void Method1 (int[] IDArray, bool SomethingElse){
....
}

When I prepare the parameters for the above function, I create and
integer array and give it some values

i.e.
int[] IDArray = new int[3]{1,2,3};

Everything works fine up to this point.

I now need to create the object array that Method.Invoke requires.

object[] Parameters = new object[2];
Parameters[0] = IDArray;
Parameters[1] = true;

Now I want to invoke the method.

MethodInfo mInfo = ......
object instance = Activator.CreateInstance(...);

mInfo.Invoke(instance,Parameters);

At this point I get an error...it tells me that Object cannot
implicitly convert to the parameter type(int[]).

All other data types and objects work fine. How can I implement
Method.Invoke to pass an array of values(not just int, it could be any
type)?

Thanks,

Neil
 
nfedin said:
I am writing a testing application that uses reflection to open
assemblies and call methods.

I have a function that looks like this...

public void Method1 (int[] IDArray, bool SomethingElse){
...
}

When I prepare the parameters for the above function, I create and
integer array and give it some values

i.e.
int[] IDArray = new int[3]{1,2,3};

Everything works fine up to this point.

I now need to create the object array that Method.Invoke requires.

object[] Parameters = new object[2];
Parameters[0] = IDArray;
Parameters[1] = true;

Now I want to invoke the method.

MethodInfo mInfo = ......
object instance = Activator.CreateInstance(...);

mInfo.Invoke(instance,Parameters);

At this point I get an error...it tells me that Object cannot
implicitly convert to the parameter type(int[]).

All other data types and objects work fine. How can I implement
Method.Invoke to pass an array of values(not just int, it could be any
type)?

It should work absolutely fine. Could you post a short but complete
program which demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Here's an example which *does* work:

using System;
using System.Reflection;

public class Test
{
public void Method1(int[] IDArray, bool SomethingElse)
{
foreach (int i in IDArray)
{
Console.WriteLine (i);
}
}

static void Main()
{
object instance = Activator.CreateInstance(typeof(Test));
MethodInfo method = typeof(Test).GetMethod("Method1");

int[] IDArray = new int[3]{1,2,3};
object[] Parameters = new object[2];
Parameters[0] = IDArray;
Parameters[1] = true;

method.Invoke(instance, Parameters);
}
}
 
Thanks Jon for the quick reply.

I didn't give all the details because I didn't want to confuse everyone
right away.

Change
int[] IDArray = new int[3]{1,2,3};
to
int[] IDArray = new object[3]{1,2,3};

and you will get an error, "Object type cannot be converted to target
type."


I want to use object, because I won't know until runtime what type the
array holds. I could be int[], string[], CustomClass[], DataSet[], etc.
I use the Type class to determine what is actually in the array.
 
I want to use object, because I won't know until runtime what type the
array holds. I could be int[], string[], CustomClass[], DataSet[], etc.
I use the Type class to determine what is actually in the array.

In that case perhaps you should consider using Array.CreateInstance()
instead to create the array, and Array.SetValue() to popluate it.



Mattias
 
n f said:
Thanks Jon for the quick reply.

I didn't give all the details because I didn't want to confuse everyone
right away.

Change
int[] IDArray = new int[3]{1,2,3};
to
int[] IDArray = new object[3]{1,2,3};

and you will get an error, "Object type cannot be converted to target
type."

Yes, and that's understandable.
I want to use object, because I won't know until runtime what type the
array holds. I could be int[], string[], CustomClass[], DataSet[], etc.
I use the Type class to determine what is actually in the array.

Then you need to create the array using Array.CreateInstance. An
object[] *isn't* an int[], so you can't use it as if it is, even it
contains only ints.
 
Back
Top