PageRenderTime 64ms CodeModel.GetById 59ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/src/LinFu.IoC/Interceptors/BaseInterceptor.cs

http://github.com/philiplaureano/LinFu
C# | 66 lines | 27 code | 8 blank | 31 comment | 0 complexity | 78938f4e34cff56500a96d639cf278cb MD5 | raw file
 1using System.Reflection;
 2using LinFu.AOP.Interfaces;
 3using LinFu.IoC.Configuration;
 4using LinFu.IoC.Configuration.Interfaces;
 5
 6namespace LinFu.IoC.Interceptors
 7{
 8    /// <summary>
 9    ///     A class that provides the most basic functionality for an interceptor.
10    /// </summary>
11    public abstract class BaseInterceptor : IInterceptor
12    {
13        /// <summary>
14        ///     The default constructor.
15        /// </summary>
16        protected BaseInterceptor()
17        {
18            MethodInvoker = new MethodInvoke();
19        }
20
21        /// <summary>
22        ///     Initializes the class with the <paramref name="methodInvoke" /> instance.
23        /// </summary>
24        /// <param name="methodInvoke">The <see cref="IMethodInvoke{TMethod}" /> instance that will invoke the target method.</param>
25        protected BaseInterceptor(IMethodInvoke<MethodInfo> methodInvoke)
26        {
27            MethodInvoker = methodInvoke;
28        }
29
30        /// <summary>
31        ///     Gets the value indicating the <see cref="IMethodInvoke{TMethod}" /> instance
32        ///     that will be used to invoke the target method.
33        /// </summary>
34        protected IMethodInvoke<MethodInfo> MethodInvoker { get; }
35
36
37        /// <summary>
38        ///     Intercepts a method call using the given
39        ///     <see cref="IInvocationInfo" /> instance.
40        /// </summary>
41        /// <param name="info">
42        ///     The <see cref="IInvocationInfo" /> instance that will
43        ///     contain all the necessary information associated with a
44        ///     particular method call.
45        /// </param>
46        /// <returns>
47        ///     The return value of the target method. If the return type of the target
48        ///     method is <see cref="void" />, then the return value will be ignored.
49        /// </returns>
50        public virtual object Intercept(IInvocationInfo info)
51        {
52            var target = GetTarget(info);
53            var method = info.TargetMethod;
54            var arguments = info.Arguments;
55
56            return MethodInvoker.Invoke(target, (MethodInfo) method, arguments);
57        }
58
59
60        /// <summary>
61        ///     Gets the target object instance.
62        /// </summary>
63        /// <param name="info">The <see cref="IInvocationInfo" /> instance that describes the current execution context.</param>
64        protected abstract object GetTarget(IInvocationInfo info);
65    }
66}