Beispiel #1
0
        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(string.Format(
                                                "Intercepted service type {0} is not a supported: it is nor class nor interface", serviceType));
            }

            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);
        }
Beispiel #2
0
            public object CreateClassProxy(Type typeToProxy, Type[] additionalInterfaces,
                                           ProxyGenerationOptions options,
                                           object[] constructorArguments,
                                           IInterceptor interceptor,
                                           ProxyBuilder builder)
            {
                if (builder == null)
                {
                    return(CreateClassProxy(typeToProxy, additionalInterfaces, options, constructorArguments, interceptor));
                }

                var proxyType = ProxyBuilder.CreateClassProxyType(typeToProxy, additionalInterfaces, options);
                var arguments = BuildArgumentListForClassProxy(options, new[] { interceptor });

                if (constructorArguments != null && constructorArguments.Length != 0)
                {
                    arguments.AddRange(constructorArguments);
                }

                return(builder(proxyType, arguments.ToArray()));
            }
Beispiel #3
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>();
            }
        }