Sto cercando di emettere un metodo che crea un'istanza di uno System.Lazy e non riuscendo con un errore PEVerify di "Token non valido", alla linea newobj instance void class [mscorlib]System.Lazy`1<class Example.ExpensiveType>::.ctor(class [mscorlib]System.Func`1<class Example.ExpensiveType>)
Emit chiamata a System.Lazy <T> costruttore con Mono.Cecil
Guardando altrove con ILDASM, vedo che una chiamata corretta sarebbe simile a questa:
newobj instance void class [mscorlib]System.Lazy`1<class Example.IHeater>::.ctor(class [mscorlib]System.Func`1<!0>)
Purtroppo, io sono in perdita su come riprodurre questo con l'API Mono.Cecil. Qualcuno può aiutare con i generici?
Ecco quello che ho finora:
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);
Qualsiasi aiuto sarebbe molto apprezzato - Sono perplesso!