-1

我得到参数计数不匹配异常。如何解决WindowsForms C#中的System.Reflection.TargetParameterCountException?

未处理的异常:System.Reflection.TargetParameterCountException:参数计数不匹配。

我对调用MethodInfo的基础代码部分如下

Type customerType = executingAssembly.GetType("LateBinding.Customer"); 
     object customerInstance = Activator.CreateInstance(customerType); 
     MethodInfo method = customerType.GetMethod("printCustomerDetails"); 
     string customerObject = (string)method.Invoke(customerInstance, new object[0]); 

我试图调用下面的方法

public string printCustomerDetails(object parameters) 
    { 
     string CustomerName = ""; 
     foreach (object customer in parameters) 
     { 
      CustomerName = CustomerName + " " + customer; 
     } 
     return CustomerName.Trim(); 
    } 

有什么我错过了调用MethodInfo的基地?

+0

您应该检查printCustomerDetails方法的参数和相同数量的参数调用。 –

+0

请显示对象声明。 – 2017-06-19 06:39:09

+0

@TAHASULTANTEMURI我已经添加了代码。 – Arulpriya

回答

0

Method.Invoke第二个参数需要的阵列,但在代码内

string customerObject = (string)method.Invoke(customerInstance, new object[0]); 

(新的对象[0]);不返回array.Its printCustomerDetails 哪些返回数组。

,所以你需要

string customerObject = (string)method.Invoke(customerInstance, Details(0));