/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

  1. using LinFu.AOP.Cecil.Interfaces;
  2. using LinFu.AOP.Interfaces;
  3. using LinFu.Reflection.Emit;
  4. using Mono.Cecil.Cil;
  5. namespace LinFu.AOP.Cecil
  6. {
  7. /// <summary>
  8. /// Represents a class that emits the call to obtain the <see cref="IAroundInvokeProvider" /> instance.
  9. /// </summary>
  10. public class GetAroundInvokeProvider : IInstructionEmitter
  11. {
  12. private readonly VariableDefinition _aroundInvokeProvider;
  13. private readonly string _providerName;
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="GetAroundInvokeProvider" /> class.
  16. /// </summary>
  17. /// <param name="aroundInvokeProvider">The local variable that holds the <see cref="IAroundInvokeProvider" /> instance.</param>
  18. /// <param name="providerName">The name of the <see cref="IAroundInvokeProvider" /> property.</param>
  19. public GetAroundInvokeProvider(VariableDefinition aroundInvokeProvider, string providerName)
  20. {
  21. _aroundInvokeProvider = aroundInvokeProvider;
  22. _providerName = providerName;
  23. }
  24. /// <summary>
  25. /// Emits the call to obtain the <see cref="IAroundInvokeProvider" /> instance.
  26. /// </summary>
  27. /// <param name="IL">The <see cref="ILProcessor" /> pointing to the target method body.</param>
  28. public void Emit(ILProcessor IL)
  29. {
  30. var method = IL.Body.Method;
  31. var module = method.DeclaringType.Module;
  32. // var aroundInvokeProvider = this.AroundInvokeProvider;
  33. var propertyName = string.Format("get_{0}", _providerName);
  34. var getAroundInvokeProvider = module.ImportMethod<IAroundInvokeHost>(propertyName);
  35. if (!method.HasThis)
  36. {
  37. IL.Emit(OpCodes.Ldnull);
  38. IL.Emit(OpCodes.Stloc, _aroundInvokeProvider);
  39. return;
  40. }
  41. IL.Emit(OpCodes.Ldarg_0);
  42. IL.Emit(OpCodes.Callvirt, getAroundInvokeProvider);
  43. IL.Emit(OpCodes.Stloc, _aroundInvokeProvider);
  44. }
  45. }
  46. }