This project is read-only.

Creating method references for ref-parameters

Topics: Metadata Model
Jun 20, 2012 at 9:35 AM


Currently I'm trying to wrap the Threading.Monitor.Enter method. Therefore I want to use the ILWriter to search and replace this method with my own wrapper method. Because my wrapper method will need the same parameter types as the original method I need to create a MethodReference object with a boolean ref-parameter as an expected parameter type.

To get to the point: how do I create a method reference for a method that accepts ref-parameters?

Here is a snippet of my current approach where I'm trying to initialize the method reference:

private void InitMonitorStartReference()
            this.wrapperMonitorStart = new Microsoft.Cci.MethodReference(
               host, this.interceptionsRef,
               0, platformType.SystemObject, platformType.SystemBoolean); //Should be ref System.Boolean

Basically I want the last argument to be a "ref bool" instead of a simple boolean.

Jun 20, 2012 at 4:20 PM

You need to use the constructor that takes IParameterTypeInformation objects rather than just ITypeReference objects. Here is a relevant snippet from one of the samples:


this.monitorEnter = new MethodReference(, systemThreadingMonitor, CallingConvention.Default, this


), 0, parameters);

parameters[0] =

new SimpleParameterTypeInformation(monitorEnter, 0, this


parameters[1] =

new SimpleParameterTypeInformation(monitorEnter, 1,, isByReference: true


Jun 21, 2012 at 2:51 PM

I can't believe how I overlooked this sample...

But thank you very much. It works like a charm.