public static void Intercept <TService, TInterceptor>(this IRegistrator registrator, object serviceKey = null) where TInterceptor : class, IInterceptor { var serviceType = typeof(TService); Type proxyType; if (serviceType.IsInterface()) { proxyType = ProxyBuilder.CreateInterfaceProxyTypeWithTargetInterface( serviceType, ArrayTools.Empty <Type>(), ProxyGenerationOptions.Default); } else if (serviceType.IsClass()) { proxyType = ProxyBuilder.CreateClassProxyType( serviceType, ArrayTools.Empty <Type>(), ProxyGenerationOptions.Default); } else { throw new ArgumentException( $"Intercepted service type {serviceType} is not a supported, cause it is nor a class nor an interface"); } var decoratorSetup = serviceKey == null ? Setup.DecoratorWith(useDecorateeReuse : true) : Setup.DecoratorWith(r => serviceKey.Equals(r.ServiceKey), useDecorateeReuse: true); registrator.Register(serviceType, proxyType, made: Made.Of(type => type.PublicConstructors().SingleOrDefault(c => c.GetParameters().Length != 0), Parameters.Of.Type <IInterceptor[]>(typeof(TInterceptor[]))), setup: decoratorSetup); }
public void LoadAssemblyIntoCache_DifferentGenerationOptions() { var savedScope = new ModuleScope(true); var builder = new DefaultProxyBuilder(savedScope); var options1 = new ProxyGenerationOptions(); options1.AddMixinInstance(new DateTime()); var options2 = ProxyGenerationOptions.Default; var cp1 = builder.CreateClassProxyType(typeof(object), Type.EmptyTypes, options1); var cp2 = builder.CreateClassProxyType(typeof(object), Type.EmptyTypes, options2); Assert.AreNotSame(cp1, cp2); Assert.AreSame( cp1, builder.CreateClassProxyType(typeof(object), Type.EmptyTypes, options1) ); Assert.AreSame( cp2, builder.CreateClassProxyType(typeof(object), Type.EmptyTypes, options2) ); var path = savedScope.SaveAssembly(); CrossAppDomainCaller.RunInOtherAppDomain( delegate(object[] args) { var newScope = new ModuleScope(false); var newBuilder = new DefaultProxyBuilder(newScope); var assembly = Assembly.LoadFrom((string)args[0]); newScope.LoadAssemblyIntoCache(assembly); var newOptions1 = new ProxyGenerationOptions(); newOptions1.AddMixinInstance(new DateTime()); var newOptions2 = ProxyGenerationOptions.Default; var loadedCP1 = newBuilder.CreateClassProxyType( typeof(object), Type.EmptyTypes, newOptions1 ); var loadedCP2 = newBuilder.CreateClassProxyType( typeof(object), Type.EmptyTypes, newOptions2 ); Assert.AreNotSame(loadedCP1, loadedCP2); Assert.AreEqual(assembly, loadedCP1.Assembly); Assert.AreEqual(assembly, loadedCP2.Assembly); }, path ); File.Delete(path); }
public void CacheMappingsHoldTypes() { ModuleScope scope = new ModuleScope(true); DefaultProxyBuilder builder = new DefaultProxyBuilder(scope); Type cp = builder.CreateClassProxyType(typeof(object), Type.EmptyTypes, ProxyGenerationOptions.Default); string savedPath = scope.SaveAssembly(); CrossAppDomainCaller.RunInOtherAppDomain(delegate(object[] args) { Assembly assembly = Assembly.LoadFrom((string)args[0]); CacheMappingsAttribute attribute = (CacheMappingsAttribute) assembly.GetCustomAttributes(typeof(CacheMappingsAttribute), false)[0]; Dictionary <CacheKey, string> entries = attribute.GetDeserializedMappings(); Assert.AreEqual(1, entries.Count); CacheKey key = new CacheKey(typeof(object), new Type[0], ProxyGenerationOptions.Default); Assert.IsTrue(entries.ContainsKey(key)); Assert.AreEqual(args[1], entries[key]); }, savedPath, cp.FullName); File.Delete(savedPath); }
public void LoadAssemblyIntoCache_DifferentGenerationOptions() { var savedScope = new ModuleScope(true); var builder = new DefaultProxyBuilder(savedScope); var options1 = new ProxyGenerationOptions(); options1.AddMixinInstance(new DateTime()); var options2 = ProxyGenerationOptions.Default; var cp1 = builder.CreateClassProxyType(typeof (object), Type.EmptyTypes, options1); var cp2 = builder.CreateClassProxyType(typeof (object), Type.EmptyTypes, options2); Assert.AreNotSame(cp1, cp2); Assert.AreSame(cp1, builder.CreateClassProxyType(typeof (object), Type.EmptyTypes, options1)); Assert.AreSame(cp2, builder.CreateClassProxyType(typeof (object), Type.EmptyTypes, options2)); var path = savedScope.SaveAssembly(); CrossAppDomainCaller.RunInOtherAppDomain(delegate(object[] args) { var newScope = new ModuleScope(false); var newBuilder = new DefaultProxyBuilder(newScope); var assembly = Assembly.LoadFrom((string) args[0]); newScope.LoadAssemblyIntoCache(assembly); var newOptions1 = new ProxyGenerationOptions(); newOptions1.AddMixinInstance(new DateTime()); var newOptions2 = ProxyGenerationOptions.Default; var loadedCP1 = newBuilder.CreateClassProxyType(typeof (object), Type.EmptyTypes, newOptions1); var loadedCP2 = newBuilder.CreateClassProxyType(typeof (object), Type.EmptyTypes, newOptions2); Assert.AreNotSame(loadedCP1, loadedCP2); Assert.AreEqual(assembly, loadedCP1.Assembly); Assert.AreEqual(assembly, loadedCP2.Assembly); }, path); File.Delete(path); }
public void CacheMappingsHoldTypes() { var scope = new ModuleScope(true); var builder = new DefaultProxyBuilder(scope); var cp = builder.CreateClassProxyType(typeof (object), Type.EmptyTypes, ProxyGenerationOptions.Default); var savedPath = scope.SaveAssembly(); CrossAppDomainCaller.RunInOtherAppDomain(delegate(object[] args) { var assembly = Assembly.LoadFrom((string) args[0]); var attribute = (CacheMappingsAttribute) assembly.GetCustomAttributes(typeof (CacheMappingsAttribute), false)[0]; var entries = attribute.GetDeserializedMappings(); Assert.AreEqual(1, entries.Count); var key = new CacheKey(typeof (object), new Type[0], ProxyGenerationOptions.Default); Assert.IsTrue(entries.ContainsKey(key)); Assert.AreEqual(args[1], entries[key]); }, savedPath, cp.FullName); File.Delete(savedPath); }