c# - 目标参数计数异常:C#
问题描述
试图通过字符串调用函数
{
object[] Parms = new object[] { "oiad", "abdj", "i" };
Type thisType = GetType();
MethodInfo theMethod = thisType.GetMethod("invo");
ParameterInfo[] parameters = theMethod.GetParameters();
if (parameters.Length != 0)
{
theMethod.Invoke(_instance, Parms);
}
}
public void invo(object[] per)
{
//
}
例外:
TargetParameterCountException: Number of parameters specified does not match the expected number.
System.Reflection.MonoMethod.ConvertValues (System.Reflection.Binder binder, System.Object[] args, System.Reflection.ParameterInfo[] pinfo, System.Globalization.CultureInfo culture, System.Reflection.BindingFlags invokeAttr) (at <7d97106330684add86d080ecf65bfe69>:0)
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <7d97106330684add86d080ecf65bfe69>:0)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters)
如果我null
用作参数:
theMethod.Invoke(_instance, null);
它工作正常。问题在于传递参数和system.object
解决方案
我你写:object[] Parms = new object[] { "oiad", "abdj", "i" };
这意味着方法 invo 的参数是: public void invo(string s1, string s2, string s3)
如果你有public void invo(object[] per)
你必须写object[] Parms = new object[] { new object[]{ "oiad", "abdj", "i"}};
推荐阅读
- javascript - 位置:带有溢出-y:auto的div内的绝对元素未显示在所需位置
- java - Bootstrap v4 不工作(Spring、Thymeleaf、Java8)
- linux - Grep,查找具有确切数量的匹配模式的行
- sinon - sinon.js: fake.rejects - 使用 calledWith 进行测试
- opengl - GLFW 应用程序启动时出现随机空白窗口(glfwSwapBuffers 无效)
- mongodb - 使用 $isolated 在 MongoDB 中进行原子查找和删除?
- python - 无法记录使用 Keras 构建但使用 Tensorflow 的 Estimator 训练的模型的损失
- arrays - 多维数据输入
- batch-file - 将文件和文件夹拖放到命令批处理中
- python - 在 python 中对子包进行测试的地方在哪里?