PageRenderTime 167ms CodeModel.GetById 58ms app.highlight 12ms RepoModel.GetById 69ms app.codeStats 0ms

/src/LinFu.AOP/Emitters/GetSurroundingImplementationInstance.cs

http://github.com/philiplaureano/LinFu
C# | 57 lines | 36 code | 6 blank | 15 comment | 0 complexity | 38f783f1ed8ae57559459b7a42dc845c MD5 | raw file
 1using LinFu.AOP.Cecil.Interfaces;
 2using LinFu.AOP.Interfaces;
 3using LinFu.Reflection.Emit;
 4using Mono.Cecil.Cil;
 5
 6namespace LinFu.AOP.Cecil
 7{
 8    /// <summary>
 9    ///     Represents a class that emits the instructions that obtain the current <see cref="IAroundInvoke" /> instance.
10    /// </summary>
11    public class GetSurroundingImplementationInstance : IInstructionEmitter
12    {
13        private readonly VariableDefinition _aroundInvokeProvider;
14        private readonly VariableDefinition _invocationInfo;
15        private readonly Instruction _skipGetSurroundingImplementation;
16        private readonly VariableDefinition _surroundingImplementation;
17
18        /// <summary>
19        ///     Initializes a new instance of the <see cref="GetSurroundingImplementationInstance" /> class.
20        /// </summary>
21        /// <param name="aroundInvokeProvider">The variable that will hold the <see cref="IAroundInvokeProvider" /> instance.</param>
22        /// <param name="invocationInfo"></param>
23        /// <param name="surroundingImplementation"></param>
24        /// <param name="skipGetSurroundingImplementation"></param>
25        public GetSurroundingImplementationInstance(VariableDefinition aroundInvokeProvider,
26            VariableDefinition invocationInfo,
27            VariableDefinition surroundingImplementation,
28            Instruction skipGetSurroundingImplementation)
29        {
30            _aroundInvokeProvider = aroundInvokeProvider;
31            _invocationInfo = invocationInfo;
32            _surroundingImplementation = surroundingImplementation;
33            _skipGetSurroundingImplementation = skipGetSurroundingImplementation;
34        }
35
36
37        /// <summary>
38        ///     Emits the instructions that obtain the current <see cref="IAroundInvoke" /> instance.
39        /// </summary>
40        /// <param name="IL"></param>
41        public void Emit(ILProcessor IL)
42        {
43            var module = IL.Body.Method.DeclaringType.Module;
44
45            IL.Emit(OpCodes.Ldloc, _aroundInvokeProvider);
46            IL.Emit(OpCodes.Brfalse, _skipGetSurroundingImplementation);
47
48            // var surroundingImplementation = this.GetSurroundingImplementation(this, invocationInfo);
49            var getSurroundingImplementation =
50                module.ImportMethod<IAroundInvokeProvider>("GetSurroundingImplementation");
51            IL.Emit(OpCodes.Ldloc, _aroundInvokeProvider);
52            IL.Emit(OpCodes.Ldloc, _invocationInfo);
53            IL.Emit(OpCodes.Callvirt, getSurroundingImplementation);
54            IL.Emit(OpCodes.Stloc, _surroundingImplementation);
55        }
56    }
57}