Verve Innovation
Verve Innovation

Reputation: 2086

C# Parameter count mismatch error while passing object array in invoke method( Reflection)

I am trying to call function dynamically here and passing argument, don't know why it throws error there.

Assembly objAssembly;
objAssembly = Assembly.GetExecutingAssembly();

//get the class type information in which late bindig applied 
Type classType = objAssembly.GetType("Project." +strClassname);

//create the instance of class using System.Activator class 
object obj = Activator.CreateInstance(classType);

//fixed object  objValue[5];/* = new object[5];
object[] _objval = new object[3];

MethodInfo mi = classType.GetMethod("perFormAction");
mi.Invoke(obj, **_objval**); // Error here ..

I don't know why it throws parameter count mismatch here.

Upvotes: 2

Views: 7866

Answers (2)

VinayC
VinayC

Reputation: 49165

Ok - so notice that the parameter to your method is a single parameter whose type is object array. Hence you need pass it in the same way. For example,

object[] _objval = new object[3];
....     // Fill the array with values to be supplied here
object[] parameters = new object[] { _objval }; // one parameter of type object array
...
mi.Invoke(obj, parameters);

Upvotes: 7

Timores
Timores

Reputation: 14589

In your real code, do you fill the _objVal array or not ? If you don't, there's possibly the issue that MethodInfo.Invoke needs to know the type of the parameters in order to find the potentially overloaded method.

And what is the signature of the perFormAction method ?

Upvotes: 0

Related Questions