/src/LinFu.AOP/Emitters/GetSurroundingClassImplementation.cs

http://github.com/philiplaureano/LinFu · C# · 50 lines · 32 code · 5 blank · 13 comment · 0 complexity · 34fec0697421ed45b21c0d029541c028 MD5 · raw file

  1. using System.Reflection;
  2. using LinFu.AOP.Cecil.Interfaces;
  3. using LinFu.AOP.Interfaces;
  4. using LinFu.Reflection.Emit;
  5. using Mono.Cecil.Cil;
  6. namespace LinFu.AOP.Cecil
  7. {
  8. /// <summary>
  9. /// Represents a class that emits the instructions that obtain the <see cref="IAroundInvoke" /> instance.
  10. /// </summary>
  11. public class GetSurroundingClassImplementation : IInstructionEmitter
  12. {
  13. private readonly MethodInfo _getSurroundingImplementationMethod;
  14. private readonly VariableDefinition _invocationInfo;
  15. private readonly VariableDefinition _surroundingClassImplementation;
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="GetSurroundingClassImplementation" /> class.
  18. /// </summary>
  19. /// <param name="invocationInfo">The variable that contains the <see cref="IInvocationInfo" /> instance.</param>
  20. /// <param name="surroundingClassImplementation">The variable that contains the <see cref="IAroundInvoke" /> instance.</param>
  21. /// <param name="getSurroundingImplementationMethod">The method that will obtain the <see cref="IAroundInvoke" /> instance.</param>
  22. public GetSurroundingClassImplementation(VariableDefinition invocationInfo,
  23. VariableDefinition surroundingClassImplementation,
  24. MethodInfo getSurroundingImplementationMethod)
  25. {
  26. _invocationInfo = invocationInfo;
  27. _surroundingClassImplementation = surroundingClassImplementation;
  28. _getSurroundingImplementationMethod = getSurroundingImplementationMethod;
  29. }
  30. /// <summary>
  31. /// Emits the instructions that obtain the <see cref="IAroundInvoke" /> instance.
  32. /// </summary>
  33. /// <param name="IL">The <see cref="ILProcessor" /> that points to the current method body.</param>
  34. public void Emit(ILProcessor IL)
  35. {
  36. var method = IL.Body.Method;
  37. var declaringType = method.DeclaringType;
  38. var module = declaringType.Module;
  39. var getSurroundingImplementation = module.Import(_getSurroundingImplementationMethod);
  40. IL.Emit(OpCodes.Ldloc, _invocationInfo);
  41. IL.Emit(OpCodes.Call, getSurroundingImplementation);
  42. IL.Emit(OpCodes.Stloc, _surroundingClassImplementation);
  43. }
  44. }
  45. }