Parameter count mismatch.
尝试在行上的另一个类中调用方法时收到错误val = (bool)method.Invoke(instance, args);
该方法只有一个参数,并且(我认为)我将单个参数作为对象传递,因此不确定为什么会出现错误。
请有人能告诉我我的代码有什么问题吗?
class firstClass
{
public bool MethodXYZ(System.Windows.Forms.WebBrowser Wb,
string debug_selectedOption)
{
object[] args = new object[] { Wb, debug_selectedOption };
string methodToInvoke = System.Reflection.MethodBase.GetCurrentMethod().Name;
return runGenericMethod(methodToInvoke, args);
}
private bool runGenericMethod(string methodToInvoke, object[] args)
{
bool val = false;
string anotherClass = args[1].ToString();
Type t = Type.GetType("ProjectXYZ." + anotherClass);
MethodInfo method = t.GetMethod(methodToInvoke);
var constructorInfo = t.GetConstructor(new Type[0]);
if (constructorInfo != null)
{
object instance = Activator.CreateInstance(t);
val = (bool)method.Invoke(instance, args);
}
//........
return val;
}
}
class anotherClass
{
public bool MethodXYZ(object[] args)
{
return true;
}
}
Invoke
采用对象数组来支持可变数量的参数。在您的情况下,您只有一个参数,该参数本身位于对象数组中。因此,您需要创建一个新的对象数组,其唯一成员是原始对象数组:
val = (bool)method.Invoke(instance, new object[] {args});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句