Private[Object|Type|Accessors] & Automatic unpacking of ref/out params

I was writing some unit tests yesterday, and needed to test a private method. As I was gleely tapping in the code to call my static method using PrivateType, I noticed that two of my parameters were "out" parameters. I immediately went 'Hmm, I bet private type doesn't do anything special for those parameters'. I cast my mind back to a bug I fixed with the new Publicize tool in VS2008 where by ref/out parameters that were actually accessor types were not being 'unwrapped' and passed back out to the test code. Turns out we weren't doing the right thing here, but in fact reflection was helping us.

Reflection, when you give it the params array to Invoke, it actually automatically unpacks (although, in reality, I don't think there is any unpacking going on) the out/ref's into the same array, so you can get back at them. This means that since Private[Type|Object] are just simple wrappers around reflection, their Invoke methods do the work for you, and in fact everything works for free.

In the case of accessors, the experience is (now) the same. But it wasn't a complete no-brainer to get it to work. See, when we call the private method from the accessor, we have to unpack all the accessor types into their raw type, to pass to the actual method. This means that when the invoke returns, the 'raw' array has those ref/out values. But before we can return to the test code (the real caller), we have to 'repack' the instances into accessor types using the attach functionality of PrivateObject. It's a bit hairy when it comes to value types rather than ref types, but ultimately it's all good.

Skip to main content