/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
- using LinFu.AOP.Cecil.Interfaces;
- using LinFu.AOP.Interfaces;
- using LinFu.Reflection.Emit;
- using Mono.Cecil.Cil;
- namespace LinFu.AOP.Cecil
- {
- /// <summary>
- /// Represents a class that emits the call to obtain the <see cref="IAroundInvokeProvider" /> instance.
- /// </summary>
- public class GetAroundInvokeProvider : IInstructionEmitter
- {
- private readonly VariableDefinition _aroundInvokeProvider;
- private readonly string _providerName;
- /// <summary>
- /// Initializes a new instance of the <see cref="GetAroundInvokeProvider" /> class.
- /// </summary>
- /// <param name="aroundInvokeProvider">The local variable that holds the <see cref="IAroundInvokeProvider" /> instance.</param>
- /// <param name="providerName">The name of the <see cref="IAroundInvokeProvider" /> property.</param>
- public GetAroundInvokeProvider(VariableDefinition aroundInvokeProvider, string providerName)
- {
- _aroundInvokeProvider = aroundInvokeProvider;
- _providerName = providerName;
- }
- /// <summary>
- /// Emits the call to obtain the <see cref="IAroundInvokeProvider" /> instance.
- /// </summary>
- /// <param name="IL">The <see cref="ILProcessor" /> pointing to the target method body.</param>
- public void Emit(ILProcessor IL)
- {
- var method = IL.Body.Method;
- var module = method.DeclaringType.Module;
- // var aroundInvokeProvider = this.AroundInvokeProvider;
- var propertyName = string.Format("get_{0}", _providerName);
- var getAroundInvokeProvider = module.ImportMethod<IAroundInvokeHost>(propertyName);
- if (!method.HasThis)
- {
- IL.Emit(OpCodes.Ldnull);
- IL.Emit(OpCodes.Stloc, _aroundInvokeProvider);
- return;
- }
- IL.Emit(OpCodes.Ldarg_0);
- IL.Emit(OpCodes.Callvirt, getAroundInvokeProvider);
- IL.Emit(OpCodes.Stloc, _aroundInvokeProvider);
- }
- }
- }