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

/src/LinFu.AOP/Emitters/GetAroundInvokeProvider.cs

http://github.com/philiplaureano/LinFu
C# | 53 lines | 33 code | 7 blank | 13 comment | 1 complexity | cc5d625903afcaa2ab11b4c1f71787bc 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 call to obtain the <see cref="IAroundInvokeProvider" /> instance.
10    /// </summary>
11    public class GetAroundInvokeProvider : IInstructionEmitter
12    {
13        private readonly VariableDefinition _aroundInvokeProvider;
14        private readonly string _providerName;
15
16        /// <summary>
17        ///     Initializes a new instance of the <see cref="GetAroundInvokeProvider" /> class.
18        /// </summary>
19        /// <param name="aroundInvokeProvider">The local variable that holds the <see cref="IAroundInvokeProvider" /> instance.</param>
20        /// <param name="providerName">The name of the <see cref="IAroundInvokeProvider" /> property.</param>
21        public GetAroundInvokeProvider(VariableDefinition aroundInvokeProvider, string providerName)
22        {
23            _aroundInvokeProvider = aroundInvokeProvider;
24            _providerName = providerName;
25        }
26
27
28        /// <summary>
29        ///     Emits the call to obtain the <see cref="IAroundInvokeProvider" /> instance.
30        /// </summary>
31        /// <param name="IL">The <see cref="ILProcessor" /> pointing to the target method body.</param>
32        public void Emit(ILProcessor IL)
33        {
34            var method = IL.Body.Method;
35            var module = method.DeclaringType.Module;
36
37            // var aroundInvokeProvider = this.AroundInvokeProvider;
38            var propertyName = string.Format("get_{0}", _providerName);
39            var getAroundInvokeProvider = module.ImportMethod<IAroundInvokeHost>(propertyName);
40
41            if (!method.HasThis)
42            {
43                IL.Emit(OpCodes.Ldnull);
44                IL.Emit(OpCodes.Stloc, _aroundInvokeProvider);
45                return;
46            }
47
48            IL.Emit(OpCodes.Ldarg_0);
49            IL.Emit(OpCodes.Callvirt, getAroundInvokeProvider);
50            IL.Emit(OpCodes.Stloc, _aroundInvokeProvider);
51        }
52    }
53}