/src/LinFu.AOP/Emitters/GetSurroundingImplementationInstance.cs

http://github.com/philiplaureano/LinFu · C# · 57 lines · 36 code · 6 blank · 15 comment · 0 complexity · 38f783f1ed8ae57559459b7a42dc845c 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 instructions that obtain the current <see cref="IAroundInvoke" /> instance.
  9. /// </summary>
  10. public class GetSurroundingImplementationInstance : IInstructionEmitter
  11. {
  12. private readonly VariableDefinition _aroundInvokeProvider;
  13. private readonly VariableDefinition _invocationInfo;
  14. private readonly Instruction _skipGetSurroundingImplementation;
  15. private readonly VariableDefinition _surroundingImplementation;
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="GetSurroundingImplementationInstance" /> class.
  18. /// </summary>
  19. /// <param name="aroundInvokeProvider">The variable that will hold the <see cref="IAroundInvokeProvider" /> instance.</param>
  20. /// <param name="invocationInfo"></param>
  21. /// <param name="surroundingImplementation"></param>
  22. /// <param name="skipGetSurroundingImplementation"></param>
  23. public GetSurroundingImplementationInstance(VariableDefinition aroundInvokeProvider,
  24. VariableDefinition invocationInfo,
  25. VariableDefinition surroundingImplementation,
  26. Instruction skipGetSurroundingImplementation)
  27. {
  28. _aroundInvokeProvider = aroundInvokeProvider;
  29. _invocationInfo = invocationInfo;
  30. _surroundingImplementation = surroundingImplementation;
  31. _skipGetSurroundingImplementation = skipGetSurroundingImplementation;
  32. }
  33. /// <summary>
  34. /// Emits the instructions that obtain the current <see cref="IAroundInvoke" /> instance.
  35. /// </summary>
  36. /// <param name="IL"></param>
  37. public void Emit(ILProcessor IL)
  38. {
  39. var module = IL.Body.Method.DeclaringType.Module;
  40. IL.Emit(OpCodes.Ldloc, _aroundInvokeProvider);
  41. IL.Emit(OpCodes.Brfalse, _skipGetSurroundingImplementation);
  42. // var surroundingImplementation = this.GetSurroundingImplementation(this, invocationInfo);
  43. var getSurroundingImplementation =
  44. module.ImportMethod<IAroundInvokeProvider>("GetSurroundingImplementation");
  45. IL.Emit(OpCodes.Ldloc, _aroundInvokeProvider);
  46. IL.Emit(OpCodes.Ldloc, _invocationInfo);
  47. IL.Emit(OpCodes.Callvirt, getSurroundingImplementation);
  48. IL.Emit(OpCodes.Stloc, _surroundingImplementation);
  49. }
  50. }
  51. }