Pass parameters to PrivateObject method

eomeroff picture eomeroff · Mar 18, 2014 · Viewed 16k times · Source

I am trying to unit test private method. I saw example below on this question

Class target = new Class();
PrivateObject obj = new PrivateObject(target);
var retVal = obj.Invoke("PrivateMethod");
Assert.AreEqual(retVal);

My private method has 2 ref params. How to pass them?

Answer

Lee picture Lee · Mar 18, 2014

If you pass the argument array, then any ref parameters will be populated in place:

bool p1 = true; // can be others values
bool p2 = false; // can be others values
object[] args = new object[2] { p1, p2 };
var retval = obj.Invoke("PrivateMethod", args);

p1 = (bool)args[0];
p2 = (bool)args[1];