PageRenderTime 6ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/LinFu.IoC/Interceptors/Redirector.cs

http://github.com/philiplaureano/LinFu
C# | 55 lines | 37 code | 8 blank | 10 comment | 2 complexity | e5929afe654f0b55b5322d9a4fb94c11 MD5 | raw file
 1using System;
 2using System.Reflection;
 3using LinFu.AOP.Interfaces;
 4using LinFu.IoC.Configuration.Interfaces;
 5using LinFu.Proxy.Interfaces;
 6
 7namespace LinFu.IoC.Interceptors
 8{
 9    /// <summary>
10    ///     An interceptor class that redirects calls to another interceptor.
11    /// </summary>
12    internal class Redirector : BaseInterceptor
13    {
14        private readonly Func<object> _getActualTarget;
15        private readonly IInterceptor _interceptor;
16        private readonly IProxyFactory _proxyFactory;
17
18        public Redirector(Func<object> getActualTarget, IInterceptor targetInterceptor, IProxyFactory factory,
19            IMethodInvoke<MethodInfo> methodInvoke)
20            : base(methodInvoke)
21        {
22            _getActualTarget = getActualTarget;
23            _interceptor = targetInterceptor;
24            _proxyFactory = factory;
25        }
26
27        public override object Intercept(IInvocationInfo info)
28        {
29            // Instead of using the proxy as the target,
30            // modify the InvocationInfo to show the actual target
31            var proxyType = _proxyFactory.CreateProxyType(typeof(IInvocationInfo), new Type[0]);
32            var infoProxy = Activator.CreateInstance(proxyType) as IProxy;
33
34            if (infoProxy == null)
35                return base.Intercept(info);
36
37            var modifiedInfo = (IInvocationInfo) infoProxy;
38
39            // Replace the proxy target with the actual target
40            var infoInterceptor = new InvocationInfoInterceptor(info, _getActualTarget, MethodInvoker);
41            infoProxy.Interceptor = infoInterceptor;
42
43            return _interceptor.Intercept(modifiedInfo);
44        }
45
46        /// <summary>
47        ///     Gets the target object instance.
48        /// </summary>
49        /// <param name="info">The <see cref="IInvocationInfo" /> instance that describes the current execution context.</param>
50        protected override object GetTarget(IInvocationInfo info)
51        {
52            return _getActualTarget();
53        }
54    }
55}