Ejemplo n.º 1
0
        private void RewriteIL(string dllPath, MethodDefinition method, Action <object> cb)
        {
            var instructions       = method.Body.Instructions.ToList();
            var targetInstructions = GetInstructionsWhichNeedsRewrite(instructions, cb);

            ClearMethodBodyInstructions(method);
            var processor = method.Body.GetILProcessor();

            foreach (var instruction in instructions)
            {
                foreach (var generatedInstruction in GenerateInstrumentAfterAddingCoverageHook(
                             targetInstructions,
                             instruction, method, processor))
                {
                    if (generatedInstruction == null)
                    {
                        continue;
                    }

                    var sequencePoint = method.DebugInformation.GetSequencePoint(instruction);
                    var typeName      = method.DeclaringType.Name;
                    CodeVisitor.AppendCodeInstrument(processor, sequencePoint, "coverage.txt", typeName, method.Name);

                    if (InstructionInTargetInstructionList(instruction, targetInstructions))
                    {
                        OverrideInstructionValues(targetInstructions, instruction);
                    }
                    else
                    {
                        processor.Append(instruction);
                    }
                }
            }
        }