Esempio n. 1
0
        public static void Intercept <TInterceptor>(this IRegistrator registrator, Type serviceType, object serviceKey = null)
            where TInterceptor : class, IInterceptor
        {
            Ensure.NotNull(serviceType, nameof(serviceType));

            Type proxyType;

            if (serviceType.IsInterface())
            {
                proxyType = ProxyBuilder.CreateInterfaceProxyTypeWithTargetInterface(
                    serviceType, new Type[] { }, _generationOptions);
            }
            else if (serviceType.IsClass())
            {
                proxyType = ProxyBuilder.CreateClassProxyType(
                    serviceType, new Type[] { }, _generationOptions);
            }
            else
            {
                throw new ArgumentException(
                          $"Intercepted service type {serviceType} is not a supported: it is nor class nor 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.GetPublicInstanceConstructors().SingleOrDefault(c => c.GetParameters().Length != 0),
                                               Parameters.Of.Type <IInterceptor[]>(typeof(TInterceptor[]))),
                                 setup: decoratorSetup);

            if (!registrator.IsRegistered <TInterceptor>())
            {
                registrator.Register <TInterceptor>();
            }
        }