/// <summary> /// Hijack current thread and give control to the logging library at the beginning of a method /// </summary> /// <param name="method"></param> static void InjectMethodEnterHook(MethodDef method) { var prolog = new List <Instruction>() { Instruction.Create(OpCodes.Ldstr, method.FullName), Instruction.Create(OpCodes.Call, MethodEnterRef) }; method.InjectBefore(method.Body.Instructions.First(), prolog); }
/// <summary> /// Hijack current thread and give control to the logging library just before method return /// </summary> /// <param name="method">Method to hook</param> static void InjectMethodReturnHook(MethodDef method) { var epilog = new List <Instruction>() { Instruction.Create(OpCodes.Ldstr, method.FullName), Instruction.Create(OpCodes.Call, MethodReturnRef) }; var toHook = method.Body.Instructions.Where(i => i.OpCode.Code == Code.Ret).ToArray(); foreach (var instruction in toHook) { method.InjectBefore(instruction, epilog); } }