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