/src/LinFu.Proxy/ProxyFactoryExtensions.cs
C# | 95 lines | 36 code | 6 blank | 53 comment | 0 complexity | 1109e51fa733d6906253151bd60d34e5 MD5 | raw file
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using LinFu.AOP.Interfaces; 6using LinFu.Proxy.Interfaces; 7 8namespace LinFu.Proxy 9{ 10 /// <summary> 11 /// Extends the <see cref="IProxyFactory"/> class to support 12 /// instantiating proxy types. 13 /// </summary> 14 public static class ProxyFactoryExtensions 15 { 16 /// <summary> 17 /// Uses the <paramref name="factory"/> to create a proxy instance 18 /// that directly derives from the <paramref name="instanceType"/> 19 /// and implements the given <paramref name="baseInterfaces"/>. 20 /// The <paramref name="wrapper"/> instance, in turn, will be used 21 /// to intercept the method calls made to the proxy itself. 22 /// </summary> 23 /// <param name="factory">The IProxyFactory instance that will be used to generate the proxy type.</param> 24 /// <param name="instanceType">The type that will be intercepted by the proxy.</param> 25 /// <param name="wrapper">The <see cref="IInvokeWrapper"/> instance that will be used to intercept method calls made to the proxy.</param> 26 /// <param name="baseInterfaces">The additional list of interfaces that the proxy will implement.</param> 27 /// <returns>A valid proxy instance.</returns> 28 public static object CreateProxy(this IProxyFactory factory, Type instanceType, 29 IInvokeWrapper wrapper, params Type[] baseInterfaces) 30 { 31 // Convert the wrapper to an IInterceptor instance. 32 var adapter = new CallAdapter(wrapper); 33 return factory.CreateProxy(instanceType, adapter, baseInterfaces); 34 } 35 36 /// <summary> 37 /// Uses the <paramref name="factory"/> to create a proxy instance 38 /// that directly derives from the <paramref name="instanceType"/> 39 /// and implements the given <paramref name="baseInterfaces"/>. 40 /// The <paramref name="interceptor"/> instance, in turn, will be used 41 /// to intercept the method calls made to the proxy itself. 42 /// </summary> 43 /// <param name="factory">The IProxyFactory instance that will be used to generate the proxy type.</param> 44 /// <param name="instanceType">The type that will be intercepted by the proxy.</param> 45 /// <param name="interceptor">The <see cref="IInterceptor"/> instance that will be used to intercept method calls made to the proxy.</param> 46 /// <param name="baseInterfaces">The additional list of interfaces that the proxy will implement.</param> 47 /// <returns>A valid proxy instance.</returns> 48 public static object CreateProxy(this IProxyFactory factory, Type instanceType, 49 IInterceptor interceptor, params Type[] baseInterfaces) 50 { 51 var proxyType = factory.CreateProxyType(instanceType, baseInterfaces); 52 var proxyInstance = (IProxy)Activator.CreateInstance(proxyType); 53 54 proxyInstance.Interceptor = interceptor; 55 56 return proxyInstance; 57 } 58 59 /// <summary> 60 /// Uses the <paramref name="factory"/> to create a proxy instance 61 /// that directly derives from the <typeparamref name="T"/> type 62 /// and implements the given <paramref name="baseInterfaces"/>. 63 /// The <paramref name="wrapper"/> instance, in turn, will be used 64 /// to intercept the method calls made to the proxy itself. 65 /// </summary> 66 /// <typeparam name="T">The type that will be intercepted by the proxy.</typeparam> 67 /// <param name="factory">The IProxyFactory instance that will be used to generate the proxy type.</param> 68 /// <param name="wrapper">The <see cref="IInvokeWrapper"/> instance that will be used to intercept method calls made to the proxy.</param> 69 /// <param name="baseInterfaces">The additional list of interfaces that the proxy will implement.</param> 70 /// <returns>A valid proxy instance.</returns> 71 public static T CreateProxy<T>(this IProxyFactory factory, IInvokeWrapper wrapper, 72 params Type[] baseInterfaces) 73 { 74 return (T) factory.CreateProxy(typeof (T), wrapper, baseInterfaces); 75 } 76 77 /// <summary> 78 /// Uses the <paramref name="factory"/> to create a proxy instance 79 /// that directly derives from the <typeparamref name="T"/> type 80 /// and implements the given <paramref name="baseInterfaces"/>. 81 /// The <paramref name="interceptor"/> instance, in turn, will be used 82 /// to intercept the method calls made to the proxy itself. 83 /// </summary> 84 /// <typeparam name="T">The type that will be intercepted by the proxy.</typeparam> 85 /// <param name="factory">The IProxyFactory instance that will be used to generate the proxy type.</param> 86 /// <param name="interceptor">The <see cref="IInterceptor"/> instance that will be used to intercept method calls made to the proxy.</param> 87 /// <param name="baseInterfaces">The additional list of interfaces that the proxy will implement.</param> 88 /// <returns>A valid proxy instance.</returns> 89 public static T CreateProxy<T>(this IProxyFactory factory, IInterceptor interceptor, 90 params Type[] baseInterfaces) 91 { 92 return (T) factory.CreateProxy(typeof (T), interceptor, baseInterfaces); 93 } 94 } 95}