Search code examples
reflectionparameter-passinginvokeref

How can take ref parameter using Invoke with Reflection


How can take ref parameter(OutputData) using Invoke with Reflection. objectValues is an object array that take an parameters is sending parameter to "FM_DEC_ENC" Method.This method contains 8 parameter. One of them is outputData re parameter

 mInfo.Invoke("FM_DEC_ENC", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
                 null, objectValues,null)

public Int32 FM_DEC_ENC(String AdapterID, String SlotID, String InputData, 
        String DecKeyName, String EncKeyName, HsmCommConstants.ENCRIPTION DecEcbOrCbc,
        HsmCommConstants.ENCRIPTION EncEcbOrCbc, ref String OutputData)


Solution

  • You are already doing it correctly by keeping a reference to the parameter array. When the method returns, the result will be in objectValues[7].

    Note that this won't work if FM_DEC_ENC is a native method that expects a modifiable buffer as OutputData (because System.String cannot be modified). In this case, you can also specify StringBuilder as parameter type.