PageRenderTime 437ms CodeModel.GetById 10ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 96ms

/src/LinFu.Proxy/ProxyImplementor.cs

http://github.com/philiplaureano/LinFu
C# | 37 lines | 23 code | 3 blank | 11 comment | 1 complexity | 9b5292d9ca221e7e0186f5576e7a189c MD5 | raw file
 1using LinFu.AOP.Interfaces;
 2using LinFu.IoC.Configuration;
 3using LinFu.Proxy.Interfaces;
 4using LinFu.Reflection.Emit;
 5using LinFu.Reflection.Emit.Interfaces;
 6using Mono.Cecil;
 7using CallingConventions = Mono.Cecil.MethodCallingConvention;
 8
 9namespace LinFu.Proxy
10{
11    /// <summary>
12    ///     A class that provides the default implementation
13    ///     for the IProxy interface.
14    /// </summary>
15    [Implements(typeof(ITypeBuilder), LifecycleType.OncePerRequest, ServiceName = "ProxyImplementor")]
16    internal class ProxyImplementor : ITypeBuilder
17    {
18        /// <summary>
19        ///     Constructs a type that implements the
20        ///     <see cref="IProxy" /> interface.
21        /// </summary>
22        /// <param name="module">The module that will hold the target type.</param>
23        /// <param name="targetType">The type that will implement the <see cref="IProxy" /> interface.</param>
24        public void Construct(ModuleDefinition module, TypeDefinition targetType)
25        {
26            var proxyInterfaceType = module.Import(typeof(IProxy));
27            var interceptorType = module.Import(typeof(IInterceptor));
28
29            // Implement the IProxy interface only once
30            if (targetType.Interfaces.Contains(proxyInterfaceType))
31                return;
32
33            targetType.Interfaces.Add(proxyInterfaceType);
34            targetType.AddProperty("Interceptor", interceptorType);
35        }
36    }
37}