I'm trying to emit a method that instantiates a System.Lazy and failing with a PEVerify error of "Invalid token", at the line newobj instance void class [mscorlib]System.Lazy`1<class Example.ExpensiveType>::.ctor(class [mscorlib]System.Func`1<class Example.ExpensiveType>)
Looking elsewhere with ILDasm, I see that a proper call would look like this:
newobj instance void class [mscorlib]System.Lazy`1<class Example.IHeater>::.ctor(class [mscorlib]System.Func`1<!0>)
Unfortunately, I'm at a loss as to how to reproduce this with the Mono.Cecil API. Can someone help with the generics?
Here's what I have thus far:
var get = new MethodDefinition(
"Get",
MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual,
ModuleDefinition.TypeSystem.Object);
var funcType = new GenericInstanceType(ImportedTypes.FuncOfT);
funcType.GenericArguments.Add(lazyElementType);
var funcTypeCtor = new MethodReference(".ctor", ModuleDefinition.TypeSystem.Void, funcType);
funcTypeCtor.Parameters.Add(new ParameterDefinition(ModuleDefinition.TypeSystem.Object));
funcTypeCtor.Parameters.Add(new ParameterDefinition(ModuleDefinition.TypeSystem.IntPtr));
funcTypeCtor.HasThis = true;
funcTypeCtor = ModuleDefinition.Import(funcTypeCtor);
var lazyTypeCtor = new MethodReference(".ctor", ModuleDefinition.TypeSystem.Void, lazyType);
var parameterDefinition = new ParameterDefinition(funcType);
lazyTypeCtor.Parameters.Add(parameterDefinition);
lazyTypeCtor.HasThis = true;
lazyTypeCtor = ModuleDefinition.Import(lazyTypeCtor);
il = get.Body.GetILProcessor();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldftn, getTypedValue);
il.Emit(OpCodes.Newobj, funcTypeCtor);
il.Emit(OpCodes.Newobj, lazyTypeCtor); // This leads to the invalid token
il.Emit(OpCodes.Ret);
lazyBinding.Methods.Add(get);
Any help would be much appreciated - I'm stumped!
I've discovered the answer buried in a years-old mailing list archive (thanks to Gábor Kozár!). I was not properly creating/importing generic types and their methods. The code that properly loads the
Lazy<T>
andFunc<T>
types follows:Key to the above is the extension method
MakeHostInstanceGeneric
, which is defined as