MethodInfo 使用实体 Class 参数调用

MethodInfo Invoke With Entity Class Parameters

我有一个 class 名字 "BusinessError"。我正在尝试从控制台应用程序调用此 class (使用反射)。出现错误..(我清理visual studio多次或重新启动)

Message = “'RestService.RequestGetOrderDetail' 类型的对象无法转换为 'RestService.RequestGetOrderDetail' 类型。 “ 但是当我尝试调用 BusinessHasNoError class 时没有错误..

MethodInfo 如何调用实体 class 参数(无字符串,带参数的整数)..

public class BusinessError
{
    public ResponseGetOrderDetail Test1(RequestGetOrderDetail requestGetOrderDetail)
    {
      ResponseGetOrderDetail responseGetOrderDetail = new ResponseGetOrderDetail(
          requestGetOrderDetail);
      return responseGetOrderDetail;
    }
}


public class BusinessNoError
{
    public ResponseGetOrderDetail Test1(string id)
    {
        ResponseGetOrderDetail responseGetOrderDetail = new ResponseGetOrderDetail(id);
        return responseGetOrderDetail;
    }
}


public string Execute(DllInfo dllInfo)
{
      object[] parameterObject = new object[1];
      parameterObject[0] = "O19082900055";
      string dllsPath = @"C:\Scheduler\DLLs\" + dllInfo.Dll;
      string nameSpace = dllInfo.NameSpace;
      string className = dllInfo.Class;
      string methodName = dllInfo.Method;
      object result = string.Empty;
      RequestGetOrderDetail requestGetOrderDetail = new RequestGetOrderDetail();
      requestGetOrderDetail.OrderID = "O19082900055";
      parameterObject[0] = new RequestGetOrderDetail() { OrderID = "O19082900055" };
      try
      {
        Assembly assembly = Assembly.LoadFrom(dllsPath);
        Type typeInstance = assembly.GetType(nameSpace + "." + className);

        if (typeInstance != null)
        {
          MethodInfo methodInfo = typeInstance.GetMethod(methodName);
          ParameterInfo[] parameterInfo = methodInfo.GetParameters();
          object classInstance = Activator.CreateInstance(typeInstance, null);
          if (parameterInfo.Length == 0)
          {
            result = methodInfo.Invoke(classInstance, null);
          }
          else
          {
            //I want to send requestGetOrderDetail object to  methodInfo.Invoke !!!!!
            //result = methodInfo.Invoke(classInstance, requestGetOrderDetail);  
            **result = methodInfo.Invoke(classInstance, parameterObject);**
          }
        }
      }
      catch (Exception ex)
      {
        throw;
      }
      return result.ToString();
}

File Example

Invoke将参数作为对象[]; (仅此而已) 所以你的电话应该是

Invoke(classInstance, new object[] { new RequestGetOrderDetail() {OrderId=..}; });