PageRenderTime 7ms CodeModel.GetById 2ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

/src/LinFu.AOP/InstructionSwapper.cs

http://github.com/philiplaureano/LinFu
C# | 51 lines | 26 code | 4 blank | 21 comment | 1 complexity | a13ac3025a4f492910e26d2e7cb97959 MD5 | raw file
 1using System.Collections.Generic;
 2using LinFu.AOP.Cecil.Interfaces;
 3using Mono.Cecil;
 4using Mono.Cecil.Cil;
 5
 6namespace LinFu.AOP.Cecil
 7{
 8    /// <summary>
 9    ///     Provides the basic functionality for the <see cref="IMethodRewriter" /> interface.
10    /// </summary>
11    public abstract class InstructionSwapper : BaseMethodRewriter
12    {
13        /// <summary>
14        ///     Rewrites the instructions in the target method body.
15        /// </summary>
16        /// <param name="method">The target method.</param>
17        /// <param name="IL">The <see cref="ILProcessor" /> instance that represents the method body.</param>
18        /// <param name="oldInstructions">The IL instructions of the original method body.</param>
19        protected override void RewriteMethodBody(MethodDefinition method, ILProcessor IL,
20            IEnumerable<Instruction> oldInstructions)
21        {
22            var newInstructions = new Queue<Instruction>();
23            foreach (var instruction in oldInstructions)
24            {
25                if (!ShouldReplace(instruction, method))
26                {
27                    IL.Append(instruction);
28                    continue;
29                }
30
31                Replace(instruction, method, IL);
32            }
33        }
34
35        /// <summary>
36        ///     Determines whether or not the method rewriter should replace the <paramref name="oldInstruction" />.
37        /// </summary>
38        /// <param name="oldInstruction">The instruction that is currently being evaluated.</param>
39        /// <param name="hostMethod">The method that hosts the current instruction.</param>
40        /// <returns><c>true</c> if the method should be replaced; otherwise, it should return <c>false</c>.</returns>
41        protected abstract bool ShouldReplace(Instruction oldInstruction, MethodDefinition hostMethod);
42
43        /// <summary>
44        ///     Replaces the <paramref name="oldInstruction" /> with a new set of <paramref name="IL" /> instructions..
45        /// </summary>
46        /// <param name="oldInstruction">The instruction currently being evaluated.</param>
47        /// <param name="hostMethod">The method that contains the target instruction.</param>
48        /// <param name="IL">The ILProcessor for the target method body.</param>
49        protected abstract void Replace(Instruction oldInstruction, MethodDefinition hostMethod, ILProcessor IL);
50    }
51}