Reflection Array Parameter Error

N

nilesh.chitale

Hi All,
I am facing a problem while using Reflection.
I had written a simple class containing simple method taking two
parameters

1. Int Arrary
2. bool

This can be achieved by following code.

using System;
using System.Reflection;
namespace TestProject
{
public class TestClass
{
public void Method1(int[] IDArray, bool SomethingElse)
{
foreach (int i in IDArray)
{
Console.WriteLine (i);
}
}
static void Main()
{
object instance =
Activator.CreateInstance(typeof(TestClass));
MethodInfo method =
typeof(Class1).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);
}
}
}


As you can see I created a int array. But there are some scenario where
this "int" type is not known to me until runtime.
That's why I replaced the line
"int[] IDArray = new int[3]{1,2,3};"
With
"object[] IDArray = new object[3]{1,2,3};
When I tried to execute this I am getting error
"An unhandled exception of type 'System.ArgumentException' occurred
in mscorlib.dll
Additional information: Object type cannot be converted to target
type."

Am I doing something wrong here, your help in this regard will be
appreciated.

Nilesh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top