tags:

views:

108

answers:

2

HI ,I am trying to call funtion dynamically here and passing argument , dont 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 dont know why it throws parameter count mismatch here . Is anyone can help me . Thanks

A: 

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 ?

Timores
no i havent filled the _objVal ,but even after i filled with some value i still face the same problem . The signature of 'perFormAction' is public override int perFormAction( params object[] objArray)
ITion
+2  A: 

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);
VinayC
oh yeah ,you are rite vinay .. Thanks , I dint think of that .
ITion