gpt4 book ai didi

c# - 目标参数计数异常 : C#

转载 作者:行者123 更新时间:2023-12-03 08:44:57 25 4
gpt4 key购买 nike

尝试通过字符串调用函数

{
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)
{
//
}

异常(exception):

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"}};

关于c# - 目标参数计数异常 : C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61855500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com