public void InjectInCatchBlock(MethodDefinition method, MethodCodeInjectingCodeProviderArgument codeProviderArgument, ILogger logger, MethodInjectionPlace injectionPlace) { Inject( method, codeProviderArgument, logger, injectionPlace, (body, newInstruction, call) => body.AddInstructionsInCatchBlock(newInstruction, call)); }
public void Inject(MethodDefinition method, MethodCodeInjectingCodeProviderArgument codeProviderArgument, ILogger logger, MethodInjectionPlace injectionPlace, CustomInstructionsInjection injectNewInstructions) { var callInfoCollection = codeProvider.GetCallInfo(codeProviderArgument, method.Module); if (!method.HasBody) { throw new ArgumentException("Method does not contain body."); } logger.Notice($"Injecting method call before exit of method '{method.FullName}'."); newInstructions.Clear(); if (injectionPlace == MethodInjectionPlace.InCatchBlock) { GenerateInstructionsForInjectedCallInTryCatchBlock(method, newInstructions, callInfoCollection[0].MethodReferenceToBeCalled, callInfoCollection[0].CallArguments); } else { GenerateInstructionsForInjectedCall(newInstructions, callInfoCollection[0].MethodReferenceToBeCalled, callInfoCollection[0].CallArguments); } foreach (var callInfo in callInfoCollection) { injectNewInstructions(method.Body, newInstructions, callInfo.MethodReferenceToBeCalled.Resolve()); } }
public void InjectBeforeExit(MethodDefinition method, MethodCodeInjectingCodeProviderArgument codeProviderArgument, ILogger logger, MethodInjectionPlace injectionPlace) { Inject( method, codeProviderArgument, logger, injectionPlace, (body, newInstruction, call) => body.AddInstructionsBeforeExit(newInstructions)); }