PageRenderTime 102ms CodeModel.GetById 40ms app.highlight 4ms RepoModel.GetById 56ms app.codeStats 0ms

/src/LinFu.AOP/Emitters/GetClassMethodReplacementProvider.cs

http://github.com/philiplaureano/LinFu
C# | 74 lines | 41 code | 8 blank | 25 comment | 0 complexity | c799ebdca609d76d846a20c30a7d74ac MD5 | raw file
 1using System;
 2using LinFu.AOP.Cecil.Interfaces;
 3using LinFu.AOP.Interfaces;
 4using LinFu.Reflection.Emit;
 5using Mono.Cecil;
 6using Mono.Cecil.Cil;
 7
 8namespace LinFu.AOP.Cecil
 9{
10    /// <summary>
11    ///     Represents a class that emits the instructions that obtain a class-level <see cref="IMethodReplacementProvider" />
12    ///     instance.
13    /// </summary>
14    public class GetClassMethodReplacementProvider : IInstructionEmitter
15    {
16        private readonly VariableDefinition _classMethodReplacementProvider;
17        private readonly VariableDefinition _invocationInfo;
18        private readonly Func<ModuleDefinition, MethodReference> _resolveGetProviderMethod;
19
20        /// <summary>
21        ///     Initializes a new instance of the <see cref="GetClassMethodReplacementProvider" /> class.
22        /// </summary>
23        /// <param name="parameters">The method body rewriter paramters that describe the </param>
24        /// <param name="resolveGetProviderMethod">
25        ///     The functor that resolves the method that obtains the method replacement
26        ///     provider instance.
27        /// </param>
28        public GetClassMethodReplacementProvider(IMethodBodyRewriterParameters parameters,
29            Func<ModuleDefinition, MethodReference> resolveGetProviderMethod)
30        {
31            _invocationInfo = parameters.InvocationInfo;
32            _classMethodReplacementProvider = parameters.ClassMethodReplacementProvider;
33            _resolveGetProviderMethod = resolveGetProviderMethod;
34        }
35
36        /// <summary>
37        ///     Initializes a new instance of the <see cref="GetClassMethodReplacementProvider" /> class.
38        /// </summary>
39        /// <param name="invocationInfo">The variable that contains the <see cref="IInvocationInfo" /> instance.</param>
40        /// <param name="classMethodReplacementProvider">The variable that contains the class method replacement provider instance.</param>
41        /// <param name="resolveGetProviderMethod">
42        ///     The functor that resolves the method that obtains the method replacement
43        ///     provider instance.
44        /// </param>
45        public GetClassMethodReplacementProvider(VariableDefinition invocationInfo,
46            VariableDefinition classMethodReplacementProvider,
47            Func<ModuleDefinition, MethodReference> resolveGetProviderMethod)
48        {
49            _invocationInfo = invocationInfo;
50            _classMethodReplacementProvider = classMethodReplacementProvider;
51            _resolveGetProviderMethod = resolveGetProviderMethod;
52        }
53
54
55        /// <summary>
56        ///     Emits the instructions that obtain a class-level <see cref="IMethodReplacementProvider" /> instance.
57        /// </summary>
58        /// <param name="IL">The <see cref="ILProcessor" /> instance that points to the instructions in the method body.</param>
59        public void Emit(ILProcessor IL)
60        {
61            var method = IL.Body.Method;
62            var module =  method.Module;
63
64            var getProvider = _resolveGetProviderMethod(module);
65
66            var pushThis = method.HasThis ? OpCodes.Ldarg_0 : OpCodes.Ldnull;
67            IL.Emit(pushThis);
68
69            IL.Emit(OpCodes.Ldloc, _invocationInfo);
70            IL.Emit(OpCodes.Call, getProvider);
71            IL.Emit(OpCodes.Stloc, _classMethodReplacementProvider);
72        }
73    }
74}