C#在调用方法中传递对象数组时的参数计数不匹配错误(反射)

Ver*_*ion 2 c# reflection

我试图在这里动态调用函数并传递参数,不知道为什么它会抛出错误.

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 ..
Run Code Online (Sandbox Code Playgroud)

我不知道为什么它会引发参数计数不匹配.

Vin*_*ayC 7

好的 - 请注意,方法的参数是一个参数,其类型是对象数组.因此,您需要以相同的方式传递它.例如,

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);
Run Code Online (Sandbox Code Playgroud)