private VariableDefinition InvokeReplacementMethod(RewriteContext context, VariableDefinition arrayVariable, bool isVoidMethod) { EventRewriteTarget eventTarget = context.Targets[0] as EventRewriteTarget; var importedReplacement = GetInterceptorMethod(context.Method, isVoidMethod, eventTarget); context.Insert(Instruction.Create(OpCodes.Ldloc, arrayVariable)); PushMethod(context, context.Method); if (eventTarget != null) { PushMethod(context, context.Method.Module.Import(eventTarget.AddMethod)); PushMethod(context, context.Method.Module.Import(eventTarget.RemoveMethod)); } context.Insert(Instruction.Create(OpCodes.Call, importedReplacement)); var interceptorResultVariable = new VariableDefinition(GetInterceptorResultType(context.Method, isVoidMethod)); context.Processor.Body.Variables.Add(interceptorResultVariable); context.Insert(Instruction.Create(OpCodes.Stloc, interceptorResultVariable)); return(interceptorResultVariable); }
private static MethodReference GetInterceptorMethod(MethodReference originalMethod, bool isVoidMethod, EventRewriteTarget eventTarget) { MethodBase replacementMethod = GetReplacementMethod(isVoidMethod, isEvent: eventTarget != null); MethodReference importedReplacement = originalMethod.Module.Import(replacementMethod); bool isGenericHookMethod = importedReplacement.HasGenericParameters; if (isGenericHookMethod) { GenericInstanceMethod boundMethod = new GenericInstanceMethod(importedReplacement); if (eventTarget != null) { boundMethod.GenericArguments.Add(originalMethod.Module.Import(eventTarget.EventHandlerType)); } if (!isVoidMethod) { TypeReference returnValue = GetReturnValue(originalMethod); boundMethod.GenericArguments.Add(returnValue); } importedReplacement = boundMethod; } return(importedReplacement); }
private static MethodReference GetInterceptorMethod(MethodReference originalMethod, bool isVoidMethod, EventRewriteTarget eventTarget) { MethodBase replacementMethod = GetReplacementMethod(isVoidMethod, isEvent: eventTarget != null); MethodReference importedReplacement = originalMethod.Module.Import(replacementMethod); bool isGenericHookMethod = importedReplacement.HasGenericParameters; if (isGenericHookMethod) { GenericInstanceMethod boundMethod = new GenericInstanceMethod(importedReplacement); if (eventTarget != null) { boundMethod.GenericArguments.Add(originalMethod.Module.Import(eventTarget.EventHandlerType)); } if (!isVoidMethod) { TypeReference returnValue = GetReturnValue(originalMethod); boundMethod.GenericArguments.Add(returnValue); } importedReplacement = boundMethod; } return importedReplacement; }