/src/LinFu.AOP/MethodBodyInterception/MethodBodyRewriterParameters.cs

http://github.com/philiplaureano/LinFu · C# · 124 lines · 48 code · 15 blank · 61 comment · 4 complexity · 1c8fd20e296cf1aeaed309c918f28f0c MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using LinFu.AOP.Interfaces;
  4. using LinFu.Reflection.Emit;
  5. using Mono.Cecil;
  6. using Mono.Cecil.Cil;
  7. namespace LinFu.AOP.Cecil
  8. {
  9. /// <summary>
  10. /// Represents the parameters used to add interception to a given method body.
  11. /// </summary>
  12. public class MethodBodyRewriterParameters : IMethodBodyRewriterParameters
  13. {
  14. private readonly ILProcessor _cilWorker;
  15. /// <summary>
  16. /// Initializes a new instance of the <see cref="MethodBodyRewriterParameters" /> class.
  17. /// </summary>
  18. /// <param name="IL">The ILProcessor that is responsible for the current method body.</param>
  19. /// <param name="oldInstructions">The value indicating the list of old instructions in the current method body.</param>
  20. /// <param name="interceptionDisabled">The value that determines whether or not interception is disabled.</param>
  21. /// <param name="invocationInfo">The local variable that will store the <see cref="IInvocationInfo" /> instance.</param>
  22. /// <param name="returnValue">The value indicating the local variable that will store the return value.</param>
  23. /// <param name="methodReplacementProvider">The <see cref="IMethodReplacementProvider" /> instance.</param>
  24. /// <param name="aroundInvokeProvider">The <see cref="IAroundInvokeProvider" /> instance.</param>
  25. /// <param name="classMethodReplacementProvider">The class-level<see cref="IMethodReplacementProvider" /> instance.</param>
  26. /// <param name="getMethodReplacementProviderMethod">The functor that resolves the GetMethodReplacementProvider method.</param>
  27. /// <param name="registryType">
  28. /// The interception registry type that will be responsible for handling class-level
  29. /// interception events.
  30. /// </param>
  31. public MethodBodyRewriterParameters(ILProcessor IL, IEnumerable<Instruction> oldInstructions,
  32. VariableDefinition interceptionDisabled,
  33. VariableDefinition invocationInfo,
  34. VariableDefinition returnValue,
  35. VariableDefinition methodReplacementProvider,
  36. VariableDefinition aroundInvokeProvider,
  37. VariableDefinition classMethodReplacementProvider,
  38. Func<ModuleDefinition, MethodReference> getMethodReplacementProviderMethod,
  39. Type registryType)
  40. {
  41. if (methodReplacementProvider.VariableType.FullName != typeof(IMethodReplacementProvider).FullName)
  42. throw new ArgumentException("methodReplacementProvider");
  43. if (aroundInvokeProvider.VariableType.FullName != typeof(IAroundInvokeProvider).FullName)
  44. throw new ArgumentException("aroundInvokeProvider");
  45. _cilWorker = IL;
  46. OldInstructions = oldInstructions;
  47. InterceptionDisabled = interceptionDisabled;
  48. InvocationInfo = invocationInfo;
  49. ReturnValue = returnValue;
  50. MethodReplacementProvider = methodReplacementProvider;
  51. AroundInvokeProvider = aroundInvokeProvider;
  52. ClassMethodReplacementProvider = classMethodReplacementProvider;
  53. GetMethodReplacementProviderMethod = getMethodReplacementProviderMethod;
  54. RegistryType = registryType;
  55. }
  56. /// <summary>
  57. /// Gets the value indicating the list of old instructions in the current method body.
  58. /// </summary>
  59. /// <value>The value indicating the list of old instructions in the current method body.</value>
  60. public IEnumerable<Instruction> OldInstructions { get; }
  61. /// <summary>
  62. /// Gets the value indicating the class-level<see cref="IMethodReplacementProvider" /> instance.
  63. /// </summary>
  64. /// <value>The class-level<see cref="IMethodReplacementProvider" /> instance.</value>
  65. public VariableDefinition ClassMethodReplacementProvider { get; }
  66. /// <summary>
  67. /// Gets the value indicating the local variable used to store the <see cref="IAroundInvokeProvider" /> instance.
  68. /// </summary>
  69. /// <value>The <see cref="IAroundInvokeProvider" /> instance.</value>
  70. public VariableDefinition AroundInvokeProvider { get; }
  71. /// <summary>
  72. /// Gets the value indicating the local variable used to store the <see cref="IMethodReplacementProvider" /> instance.
  73. /// </summary>
  74. /// <value>The <see cref="IMethodReplacementProvider" /> instance.</value>
  75. public VariableDefinition MethodReplacementProvider { get; }
  76. /// <summary>
  77. /// Gets the value indicating the TargetMethod to be modified.
  78. /// </summary>
  79. /// <value>The method to be modified.</value>
  80. public MethodDefinition TargetMethod => _cilWorker.Body.Method;
  81. /// <summary>
  82. /// Gets the value indicating the local variable that will store the value that determines whether or not
  83. /// interception is disabled.
  84. /// </summary>
  85. /// <value>The value that determines whether or not interception is disabled.</value>
  86. public VariableDefinition InterceptionDisabled { get; }
  87. /// <summary>
  88. /// Gets the value indicating the local variable that will store the <see cref="IInvocationInfo" /> instance.
  89. /// </summary>
  90. /// <value>The local variable that will store the <see cref="IInvocationInfo" /> instance.</value>
  91. public VariableDefinition InvocationInfo { get; }
  92. /// <summary>
  93. /// Gets the value indicating the local variable that will store the return value.
  94. /// </summary>
  95. /// <value>The value indicating the local variable that will store the return value.</value>
  96. public VariableDefinition ReturnValue { get; }
  97. /// <summary>
  98. /// Gets the value indicating the interception registry type that will be responsible for handling class-level
  99. /// interception events.
  100. /// </summary>
  101. /// <value>The interception registry type that will be responsible for handling class-level interception events.</value>
  102. public Type RegistryType { get; }
  103. /// <summary>
  104. /// Gets the value indicating the functor that resolves the GetMethodReplacementProvider method.
  105. /// </summary>
  106. /// <value>The functor that resolves the GetMethodReplacementProvider method.</value>
  107. public Func<ModuleDefinition, MethodReference> GetMethodReplacementProviderMethod { get; }
  108. }
  109. }