Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a GenericParameter return type for a MethodReference in Mono.Cecil?

I'm trying to reproduce the following IL using Mono.Cecil:

call !!0 [mscorlib]System.Threading.Interlocked::CompareExchange<class [System]System.ComponentModel.PropertyChangedEventHandler>(!!0&, !!0, !!0)

When I use Mono.Cecil to inspect this IL, I see that the operand of the instruction is a GenericInstanceMethod, which holds an ElementMethod of type MethodReference. This MethodReference in turn has a return type of type GenericParameter.

I'd like to create the same objects by hand, but reach a catch-22 it seems. To create a GenericParameter, I need an IGenericParameterOwner, which seems to be the very same MethodReference above. So I'd like to pass the MethodReference to the GenericParameter constructor. However, I can't create the MethodReference without a TypeReference for the return type either, which I assume should be the GenericParameter.

How do I resolve this? What I am misunderstanding?

like image 237
Einar Avatar asked Feb 24 '12 22:02

Einar


1 Answers

This seems to be an issue with the API as it is.

An easy way to work around this would be to pass a TypeReference to void as the ReturnType of your MethodReference, and change the ReturnType to the later created GenericParameter.

like image 93
Jb Evain Avatar answered Oct 29 '22 11:10

Jb Evain