private void PrepareMethodForCallCount(NetMethod method) { MethodDefinition CecilType = (MethodDefinition)method.Tag; BodyWorker worker = new BodyWorker(CecilType.Body); Instruction beforeIns = CecilType.Body.Instructions[0]; Instruction afterIns = worker.InsertBefore(beforeIns, worker.Create(OpCodes.Ldc_I4, method.Handle)); Instruction instruction4 = worker.AppendAfter(afterIns, worker.Create(OpCodes.Ldc_I4, Project.Settings.BuildKey)); worker.AppendAfter(instruction4, worker.Create(OpCodes.Call, _Context.StartEndMethodRef)); worker.Done(); }
private void PrepareMethodForTiming(AssemblyDefinition assembly, NetMethod method) { MethodDefinition CecilType = (MethodDefinition)method.Tag; MethodBody body = CecilType.Body; BodyWorker bw = new BodyWorker(body); VariableDefinition definition = null; if (CecilType.ReturnType.ReturnType != assembly.MainModule.TypeReferences["System.Void"]) { definition = new VariableDefinition(CecilType.ReturnType.ReturnType); body.Variables.Add(definition); } Instruction instruction6 = null; Instruction instruction7 = body.Instructions[0]; Instruction instruction8 = bw.InsertBefore(instruction7, bw.Create(OpCodes.Ldc_I4, method.Handle)); Instruction instruction9 = bw.AppendAfter(instruction8, bw.Create(OpCodes.Ldc_I4, Project.Settings.BuildKey)); bw.AppendAfter(instruction9, bw.Create(OpCodes.Call, _Context.StartMethodRef)); Instruction instruction10 = bw.OriginalInstructions[bw.OriginalInstructions.Count - 1]; Instruction instruction11 = bw.AppendAfter(instruction10, bw.Create(OpCodes.Call, _Context.EndMethodRef)); Instruction instruction12 = bw.AppendAfter(instruction11, bw.Create(OpCodes.Endfinally)); Instruction outside = body.Instructions.Outside; //gestion des methodes de debut/fin ? // TODO foreach (Instruction original in bw.OriginalInstructions) { Instruction instruction15; if (!(original.OpCode == OpCodes.Ret) || (original == instruction6)) { continue; } //return type exist if (definition != null) { //return the value instruction15 = bw.AppendAfter(instruction12, bw.Create(OpCodes.Ldloc, definition)); bw.AppendAfter(instruction15, bw.Create(OpCodes.Ret)); } else { //else quit instruction15 = bw.AppendAfter(instruction12, bw.Create(OpCodes.Ret)); } foreach (Instruction instruction16 in bw.OriginalInstructions) { if ((instruction16.OpCode == OpCodes.Ret) && (instruction16 != instruction6)) { if (definition != null) { Instruction instruction17 = bw.Replace(instruction16, bw.Create(OpCodes.Stloc, definition)); bw.AppendAfter(instruction17, bw.Create(OpCodes.Leave, instruction15)); } else { bw.Replace(instruction16, bw.Create(OpCodes.Leave, instruction15)); } } } outside = instruction15; break; } bw.RemapBodyOutside(instruction11); bw.Done(); //define the exception handling arround the existing code ExceptionHandler handler = new ExceptionHandler(ExceptionHandlerType.Finally) { TryStart = instruction7, TryEnd = instruction11, HandlerStart = instruction11, HandlerEnd = outside }; body.ExceptionHandlers.Add(handler); //if return type, be sure that variables are initialized if (definition != null) { body.InitLocals = true; } }