private static bool CanDecorate(ServiceRegistration registration, IAspectConfiguration aspectConfiguration)
        {
            var serviceType = registration.ServiceType.GetTypeInfo();
            var implType    = registration.GetImplType().GetTypeInfo();

            if (implType.IsProxy() || !implType.CanInherited())
            {
                return(false);
            }
            if (_excepts.Any(x => implType.Name.Matches(x)) || _excepts.Any(x => implType.Namespace.Matches(x)))
            {
                return(false);
            }
            if (!serviceType.CanInherited() || serviceType.IsNonAspect())
            {
                return(false);
            }

            var aspectValidator = new AspectValidatorBuilder(aspectConfiguration).Build();

            if (!aspectValidator.Validate(serviceType, true) && !aspectValidator.Validate(implType, false))
            {
                return(false);
            }
            return(true);
        }
        private static void ComponentRegistration_Activating(object sender, ActivatingEventArgs <object> e)
        {
            if (e.Instance == null || e.Instance.IsProxy())
            {
                return;
            }
            if (!(e.Component.Activator is ReflectionActivator ||
                  e.Component.Activator is DelegateActivator ||
                  e.Component.Activator is InstanceActivator))
            {
                return;
            }
            var limitType = e.Instance.GetType();

            if (!limitType.GetTypeInfo().CanInherited())
            {
                return;
            }
            if (excepts.Any(x => limitType.Name.Matches(x)) || excepts.Any(x => limitType.Namespace.Matches(x)))
            {
                return;
            }
            var services = e.Component.Services.Select(x => ((IServiceWithType)x).ServiceType).ToList();

            if (!services.All(x => x.GetTypeInfo().CanInherited()) || services.All(x => x.GetTypeInfo().IsNonAspect()))
            {
                return;
            }
            var aspectValidator = new AspectValidatorBuilder(e.Context.Resolve <IAspectConfiguration>()).Build();

            if (services.All(x => !aspectValidator.Validate(x, true)) && !aspectValidator.Validate(limitType, false))
            {
                return;
            }
            var  proxyTypeGenerator = e.Context.Resolve <IProxyTypeGenerator>();
            Type proxyType; object instance;
            var  interfaceType = services.FirstOrDefault(x => x.GetTypeInfo().IsInterface);

            if (interfaceType == null)
            {
                var baseType = services.FirstOrDefault(x => x.GetTypeInfo().IsClass) ?? limitType;
                proxyType = proxyTypeGenerator.CreateClassProxyType(baseType, limitType);
                var activator = new ReflectionActivator(proxyType, new DefaultConstructorFinder(type => type.GetTypeInfo().DeclaredConstructors.ToArray()), new MostParametersConstructorSelector(), new AParameter[0], new AParameter[0]);
                instance = activator.ActivateInstance(e.Context, e.Parameters);
            }
            else
            {
                proxyType = proxyTypeGenerator.CreateInterfaceProxyType(interfaceType, limitType);
                instance  = Activator.CreateInstance(proxyType, new object[] { e.Context.Resolve <IAspectActivatorFactory>(), e.Instance });
            }

            var propertyInjector = e.Context.Resolve <IPropertyInjectorFactory>().Create(instance.GetType());

            propertyInjector.Invoke(instance);
            e.Instance = instance;
            e.Component.RaiseActivating(e.Context, e.Parameters, ref instance);
        }
Exemplo n.º 3
0
        private void Kernel_ComponentModelCreated(ComponentModel model)
        {
            var aspectValidator = new AspectValidatorBuilder(_aspectConfiguration).Build();

            if (aspectValidator.Validate(model.Implementation, false) || model.Services.Any(x => aspectValidator.Validate(x, true)))
            {
                model.Interceptors.AddIfNotInCollection(InterceptorReference.ForType <AspectCoreInterceptor>());
            }
        }
        public void Execute(ResolveRequestContext context, Action <ResolveRequestContext> next)
        {
            next(context);
            if (context.Instance == null || context.Instance.IsProxy())
            {
                return;
            }
            if (!(context.Registration.Activator is ReflectionActivator ||
                  context.Registration.Activator is DelegateActivator ||
                  context.Registration.Activator is InstanceActivator))
            {
                return;
            }
            var limitType = context.Instance.GetType();

            if (!limitType.GetTypeInfo().CanInherited())
            {
                return;
            }
            if (excepts.Any(x => limitType.Name.Matches(x)) || excepts.Any(x => limitType.Namespace.Matches(x)))
            {
                return;
            }
            var services = context.Registration.Services.Select(x => ((IServiceWithType)x).ServiceType).ToList();

            if (!services.All(x => x.GetTypeInfo().CanInherited()) || services.All(x => x.GetTypeInfo().IsNonAspect()))
            {
                return;
            }
            var aspectValidator = new AspectValidatorBuilder(context.Resolve <IAspectConfiguration>()).Build();

            if (services.All(x => !aspectValidator.Validate(x, true)) && !aspectValidator.Validate(limitType, false))
            {
                return;
            }
            var  proxyTypeGenerator = context.Resolve <IProxyTypeGenerator>();
            Type proxyType; object instance;
            var  interfaceType = services.FirstOrDefault(x => x.GetTypeInfo().IsInterface);

            if (interfaceType == null)
            {
                var baseType = services.FirstOrDefault(x => x.GetTypeInfo().IsClass) ?? limitType;
                proxyType = proxyTypeGenerator.CreateClassProxyType(baseType, limitType);

                //Autofac.Core.Activators.Reflection.ReflectionActivator
                var constructorSelector   = new MostParametersConstructorSelector();
                var constructorFinder     = new DefaultConstructorFinder(type => type.GetTypeInfo().DeclaredConstructors.ToArray());
                var availableConstructors = constructorFinder.FindConstructors(proxyType);

                if (availableConstructors.Length == 0)
                {
                    throw new NoConstructorsFoundException(proxyType, $"No constructors on type '{proxyType}' can be found with the constructor finder '{constructorFinder}'.");
                }

                var binders = new ConstructorBinder[availableConstructors.Length];
                for (var idx = 0; idx < availableConstructors.Length; idx++)
                {
                    binders[idx] = new ConstructorBinder(availableConstructors[idx]);
                }

                var allBindings     = GetAllBindings(binders, context, context.Parameters);
                var selectedBinding = constructorSelector.SelectConstructorBinding(allBindings, context.Parameters);
                instance = selectedBinding.Instantiate();
            }
            else
            {
                proxyType = proxyTypeGenerator.CreateInterfaceProxyType(interfaceType, limitType);
                instance  = Activator.CreateInstance(proxyType, new object[] { context.Resolve <IAspectActivatorFactory>(), context.Instance });
            }

            var propertyInjector = context.Resolve <IPropertyInjectorFactory>().Create(instance.GetType());

            propertyInjector.Invoke(instance);
            context.Instance = instance;
        }