jeudi 10 juin 2021

Unable to call a method with "out" parameter using methodInfo.Invoke [duplicate]

I have a method public HandleGetDbVersionDgin with an out parameter as argument. Below is how I am getting the method info and trying to invoke the method with paramters but I am getting "Object does not match target type." exception.

MethodInfo methodInfo = type.GetMethod("HandleGetDbVersionDgin", new Type[] {
  typeof (GetDbVersionReq), typeof (EGatewayDefs.EGatewayRC).MakeByRefType(), typeof (DatabaseSettings)
});

if (methodInfo != null) {
  object result = null;
  ParameterInfo[] parameters = methodInfo.GetParameters();
  object classInstance = Activator.CreateInstance(type, null);

  if (parameters.Length == 0) {
    // This works fine
    result = methodInfo.Invoke(classInstance, null);
  } else {
    object[] parametersArray = new object[] {
      req,
      ercCode,
      dbSettings
    };

    // The invoke does NOT work;
    // it throws "Object does not match target type"             
    result = methodInfo.Invoke(methodInfo, parametersArray);
  }
}

Below is my method signature

public GetDbVersionResp HandleGetDbVersionDgin(GetDbVersionReq msg, out EGatewayDefs.EGatewayRC erc, DatabaseSettings dbSettings);




Aucun commentaire:

Enregistrer un commentaire