PageRenderTime 82ms CodeModel.GetById 79ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/src/LinFu.AOP/Emitters/GetSurroundingClassImplementation.cs

http://github.com/philiplaureano/LinFu
C# | 50 lines | 32 code | 5 blank | 13 comment | 0 complexity | 34fec0697421ed45b21c0d029541c028 MD5 | raw file
 1using System.Reflection;
 2using LinFu.AOP.Cecil.Interfaces;
 3using LinFu.AOP.Interfaces;
 4using LinFu.Reflection.Emit;
 5using Mono.Cecil.Cil;
 6
 7namespace LinFu.AOP.Cecil
 8{
 9    /// <summary>
10    ///     Represents a class that emits the instructions that obtain the <see cref="IAroundInvoke" /> instance.
11    /// </summary>
12    public class GetSurroundingClassImplementation : IInstructionEmitter
13    {
14        private readonly MethodInfo _getSurroundingImplementationMethod;
15        private readonly VariableDefinition _invocationInfo;
16        private readonly VariableDefinition _surroundingClassImplementation;
17
18        /// <summary>
19        ///     Initializes a new instance of the <see cref="GetSurroundingClassImplementation" /> class.
20        /// </summary>
21        /// <param name="invocationInfo">The variable that contains the <see cref="IInvocationInfo" /> instance.</param>
22        /// <param name="surroundingClassImplementation">The variable that contains the <see cref="IAroundInvoke" /> instance.</param>
23        /// <param name="getSurroundingImplementationMethod">The method that will obtain the <see cref="IAroundInvoke" /> instance.</param>
24        public GetSurroundingClassImplementation(VariableDefinition invocationInfo,
25            VariableDefinition surroundingClassImplementation,
26            MethodInfo getSurroundingImplementationMethod)
27        {
28            _invocationInfo = invocationInfo;
29            _surroundingClassImplementation = surroundingClassImplementation;
30            _getSurroundingImplementationMethod = getSurroundingImplementationMethod;
31        }
32
33
34        /// <summary>
35        ///     Emits the instructions that obtain the <see cref="IAroundInvoke" /> instance.
36        /// </summary>
37        /// <param name="IL">The <see cref="ILProcessor" /> that points to the current method body.</param>
38        public void Emit(ILProcessor IL)
39        {
40            var method = IL.Body.Method;
41            var declaringType = method.DeclaringType;
42            var module = declaringType.Module;
43
44            var getSurroundingImplementation = module.Import(_getSurroundingImplementationMethod);
45            IL.Emit(OpCodes.Ldloc, _invocationInfo);
46            IL.Emit(OpCodes.Call, getSurroundingImplementation);
47            IL.Emit(OpCodes.Stloc, _surroundingClassImplementation);
48        }
49    }
50}