mercredi 23 octobre 2019

MethodInfo Invoke With Entity Class Parameters

I have a class name of "BusinessError". I am trying to invoke this class from console application (Using reflection). There is a error..(I clean visual studio many times or restart)

Message = "Object of type 'RestService.RequestGetOrderDetail' cannot be converted to type 'RestService.RequestGetOrderDetail'. " But when ı try to invoke BusinessHasNoError class there is no error..

How ı can MethodInfo invoke with entity class paameters(No string,int with parameters)..

    public class BusinessError

{ public ResponseGetOrderDetail Test1(RequestGetOrderDetail requestGetOrderDetail) { ResponseGetOrderDetail responseGetOrderDetail = new ResponseGetOrderDetail(requestGetOrderDetail); return responseGetOrderDetail; } } //BusinessHasNoError class 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();
}




Aucun commentaire:

Enregistrer un commentaire