/// <summary> /// Intercept interface or abstract /// </summary> /// <typeparam name="TService"></typeparam> /// <typeparam name="TInterceptor"></typeparam> /// <param name="block"></param> /// <returns></returns> public static IFluentDecoratorStrategyConfiguration Intercept <TService, TInterceptor>( this IExportRegistrationBlock block) where TInterceptor : IInterceptor { Type decoratorType; var tService = typeof(TService); if (tService.GetTypeInfo().IsInterface) { decoratorType = ProxyBuilder.CreateInterfaceProxyTypeWithTargetInterface(tService, new Type[0], ProxyGenerationOptions.Default); } else if (tService.GetTypeInfo().IsClass) { decoratorType = ProxyBuilder.CreateClassProxyTypeWithTarget(tService, new Type[0], ProxyGenerationOptions.Default); } else { throw new Exception("Service type must be interface or class"); } return (block.ExportDecorator(decoratorType) .As(tService) .WithCtorParam <TInterceptor, IInterceptor[]>(i => new IInterceptor[] { i })); }
public static void Intercept(this IRegistrator registrator, Type serviceType, ParameterSelector interceptorsParameterSelector, object serviceKey = null) { Type proxyType; if (serviceType.IsInterface()) { proxyType = ProxyBuilder.CreateInterfaceProxyTypeWithTargetInterface( serviceType, ArrayTools.Empty <Type>(), ProxyGenerationOptions.Default); } else if (serviceType.IsClass()) { proxyType = ProxyBuilder.CreateClassProxyTypeWithTarget( 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"); } registrator.Register(serviceType, proxyType, made: Made.Of( pt => pt.PublicConstructors().FindFirst(ctor => ctor.GetParameters().Length != 0), interceptorsParameterSelector), setup: Setup.DecoratorOf(useDecorateeReuse: true, decorateeServiceKey: serviceKey)); }
public static IInjectionScope InterceptAttribute <TAttribute, TInterceptor>(this IInjectionScope scope) where TInterceptor : IInterceptor where TAttribute : Attribute { scope.Configure(c => { foreach (var strategy in scope.StrategyCollectionContainer.GetAllStrategies()) { if (strategy.ActivationType.GetTypeInfo().GetCustomAttribute <TAttribute>() != null) { Type decoratorType; var tService = strategy.ExportAs.First(); if (tService.GetTypeInfo().IsInterface) { decoratorType = ProxyBuilder.CreateInterfaceProxyTypeWithTargetInterface(tService, new Type[0], ProxyGenerationOptions.Default); } else if (tService.GetTypeInfo().IsClass) { decoratorType = ProxyBuilder.CreateClassProxyTypeWithTarget(tService, new Type[0], ProxyGenerationOptions.Default); } else { throw new Exception($"Service type must be interface or class"); } c.ExportDecorator(decoratorType).As(tService).WithCtorParam <TInterceptor, IInterceptor[]>(i => new IInterceptor[] { i }) .When.MeetsCondition((s, context) => { return(s.ActivationType.GetTypeInfo().GetCustomAttribute <TAttribute>() != null); }); } } }); return(scope); }