C# C中重载方法的MethodInfo调用#
我使用MethodInfo调用一个重载方法,该方法向我抛出异常TargetParameterCount不匹配,下面是我的代码C# C中重载方法的MethodInfo调用#,c#,C#,我使用MethodInfo调用一个重载方法,该方法向我抛出异常TargetParameterCount不匹配,下面是我的代码 public class Device { public bool Send(byte[] d, int l, int t) { return this.Send(d, 0, l, t); } public bool Send(byte[] d, int l, int t,int t) { return t
public class Device
{
public bool Send(byte[] d, int l, int t)
{
return this.Send(d, 0, l, t);
}
public bool Send(byte[] d, int l, int t,int t)
{
return true;
}
}
我在另一个类中调用这些函数
public class Device
{
public bool Send(byte[] d, int l, int t)
{
return this.Send(d, 0, l, t);
}
public bool Send(byte[] d, int l, int t,int t)
{
return true;
}
}
public class dw
{
public bool BasicFileDownload(Device device)
{
Type devType = device.GetType();
byte [] dbuf = readbuff();
MethodInfo methodSend = deviceType.GetMethods().Where(m => m.Name =="Send").Last();
object invokeSend = methodOpen.Invoke(device, new object[] {dbuf,0,10,100 });
}
}
现在我试图用4个参数调用Send,但它抛出了一个错误
public class Device
{
public bool Send(byte[] d, int l, int t)
{
return this.Send(d, 0, l, t);
}
public bool Send(byte[] d, int l, int t,int t)
{
return true;
}
}
System.Reflection.TargetParameterCountException:参数计数不匹配。
位于System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(对象对象对象、BindingFlags invokeAttr、绑定器、对象[]参数、CultureInfo区域性)
在System.Reflection.RuntimeMethodInfo.Invoke(对象obj、BindingFlags invokeAttr、绑定器绑定器、对象[]参数、CultureInfo区域性)
在System.Reflection.MethodBase.Invoke(对象obj,对象[]参数)处
在e:\sample\BDw.cs:第146行的Download.BasicDownload.BasicFileDownload(设备)中,您还可以检查参数计数
public class Device
{
public bool Send(byte[] d, int l, int t)
{
return this.Send(d, 0, l, t);
}
public bool Send(byte[] d, int l, int t,int t)
{
return true;
}
}
MethodInfo methodSend = deviceType.GetMethods()
.Where(m => m.Name == "Send" && m.GetParameters().Length==4).First();
如果您的情况比这更复杂,您可能还需要检查参数类型…您可以通过签名直接获得正确的发送方法
public class Device
{
public bool Send(byte[] d, int l, int t)
{
return this.Send(d, 0, l, t);
}
public bool Send(byte[] d, int l, int t,int t)
{
return true;
}
}
var signature = new[] {typeof (byte[]), typeof (int), typeof (int), typeof (int)};
MethodInfo methodSend = deviceType.GetMethod("Send", signature);
这比使用反射获取所有类型的方法,然后过滤它们更有效
public class Device
{
public bool Send(byte[] d, int l, int t)
{
return this.Send(d, 0, l, t);
}
public bool Send(byte[] d, int l, int t,int t)
{
return true;
}
}
您的代码无法工作,因为反射返回的方法的顺序不一定与您在代码中声明的顺序相同。您应该实际调用methodInfo上的GetParameters方法。这将为该方法提供正确的参数
public class Device
{
public bool Send(byte[] d, int l, int t)
{
return this.Send(d, 0, l, t);
}
public bool Send(byte[] d, int l, int t,int t)
{
return true;
}
}
ParameterInfo[]Meth_Params=methodOpen.GetParameters();
dynamic[]inputparams=新的动态[Meth_Params.Length];
inputparams[0]=第一个参数代码>
public class Device
{
public bool Send(byte[] d, int l, int t)
{
return this.Send(d, 0, l, t);
}
public bool Send(byte[] d, int l, int t,int t)
{
return true;
}
}
等等
public class Device
{
public bool Send(byte[] d, int l, int t)
{
return this.Send(d, 0, l, t);
}
public bool Send(byte[] d, int l, int t,int t)
{
return true;
}
}
然后将参数数组分配给调用
对象invokeSend=methodOpen.Invoke(设备,输入参数) 请参阅,您必须首先找到合适的方法(对于GetMethod
,它是第二个参数,用于指定类型数组)。您在代码中使用了两个不同的MethodInfo对象。methodOpen用于methodSend中的Send方法以外的其他方法。这可能是你的问题,除非你只是打了个错字。不客气。记住把其中一个答案标为正确答案,巴拉尼。
public class Device
{
public bool Send(byte[] d, int l, int t)
{
return this.Send(d, 0, l, t);
}
public bool Send(byte[] d, int l, int t,int t)
{
return true;
}
}