/src/LinFu.AOP/Extensions/InvocationInfoExtensions.cs

http://github.com/philiplaureano/LinFu · C# · 86 lines · 37 code · 6 blank · 43 comment · 0 complexity · 259b4b330c6b39414c209902d42f03ff MD5 · raw file

  1. using System.Reflection;
  2. using LinFu.AOP.Cecil.Interfaces;
  3. using LinFu.AOP.Interfaces;
  4. using Mono.Cecil;
  5. using Mono.Cecil.Cil;
  6. namespace LinFu.AOP.Cecil.Extensions
  7. {
  8. /// <summary>
  9. /// Adds helper methods classes that implement the <see cref="IInvocationInfo" />
  10. /// interface.
  11. /// </summary>
  12. public static class InvocationInfoExtensions
  13. {
  14. /// <summary>
  15. /// Emits the IL instructions that will store information about the method
  16. /// <paramref name="targetMethod">currently being executed</paramref>
  17. /// and stores the results into the <paramref name="invocationInfo">variable.</paramref>
  18. /// </summary>
  19. /// <param name="emitter">The <see cref="IEmitInvocationInfo" /> instance.</param>
  20. /// <param name="method">The method whose implementation will be intercepted.</param>
  21. /// <param name="targetMethod">The actual method that will contain the resulting instructions.</param>
  22. /// <param name="invocationInfo">
  23. /// The <see cref="VariableDefinition">local variable</see> that will store the current
  24. /// <see cref="IInvocationInfo" /> instance.
  25. /// </param>
  26. public static void Emit(this IEmitInvocationInfo emitter, MethodInfo method, MethodDefinition targetMethod,
  27. VariableDefinition invocationInfo)
  28. {
  29. var module = targetMethod.DeclaringType.Module;
  30. var interceptedMethod = module.Import(method);
  31. emitter.Emit(targetMethod, interceptedMethod, invocationInfo);
  32. }
  33. /// <summary>
  34. /// Invokes the currently executing method by using the <see cref="IInvocationInfo.Target" />
  35. /// as the target instance, the <see cref="IInvocationInfo.TargetMethod" /> as the method,
  36. /// and uses the <see cref="IInvocationInfo.Arguments" /> for the method
  37. /// arguments.
  38. /// </summary>
  39. /// <param name="info">The <see cref="IInvocationInfo" /> instance that contains information about the method call itself.</param>
  40. /// <returns>The return value of the method call.</returns>
  41. public static object Proceed(this IInvocationInfo info)
  42. {
  43. var targetMethod = info.TargetMethod;
  44. var target = info.Target;
  45. var arguments = info.Arguments;
  46. return targetMethod.Invoke(target, arguments);
  47. }
  48. /// <summary>
  49. /// Invokes the currently executing method by using the <paramref name="target" />
  50. /// as the target instance, the <see cref="IInvocationInfo.TargetMethod" /> as the method,
  51. /// and uses the <see cref="IInvocationInfo.Arguments" /> for the method
  52. /// arguments.
  53. /// </summary>
  54. /// <param name="info">The <see cref="IInvocationInfo" /> instance that contains information about the method call itself.</param>
  55. /// <param name="target">The target instance that will handle the method call.</param>
  56. /// <returns>The return value of the method call.</returns>
  57. public static object Proceed(this IInvocationInfo info, object target)
  58. {
  59. var targetMethod = info.TargetMethod;
  60. var arguments = info.Arguments;
  61. return targetMethod.Invoke(target, arguments);
  62. }
  63. /// <summary>
  64. /// Invokes the currently executing method by using the <paramref name="target" />
  65. /// as the target instance, the <see cref="IInvocationInfo.TargetMethod" /> as the method,
  66. /// and uses the <paramref name="arguments" /> for the method
  67. /// arguments.
  68. /// </summary>
  69. /// <param name="info">The <see cref="IInvocationInfo" /> instance that contains information about the method call itself.</param>
  70. /// <param name="target">The target instance that will handle the method call.</param>
  71. /// <param name="arguments">The arguments that will be used for the actual method call.</param>
  72. /// <returns>The return value of the method call.</returns>
  73. public static object Proceed(this IInvocationInfo info, object target,
  74. params object[] arguments)
  75. {
  76. var targetMethod = info.TargetMethod;
  77. return targetMethod.Invoke(target, arguments);
  78. }
  79. }
  80. }