PageRenderTime 106ms CodeModel.GetById 100ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

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