Exemple #1
0
        private static Expression TryCreatePropertySetterExpression(
            PropertyInfo property,
            IContainer container,
            ConstructionFactorySettings settings,
            ParameterExpression contextExpression,
            ParameterExpression serviceExpression)
        {
            var activator = TryGetActivator(property.PropertyType, property.Name, container, settings);

            if (activator == null)
            {
                return(null);
            }

            var propertyValueExpression = CreateActivationExpression(activator, contextExpression);
            var setterExpression        = Expression.Call(serviceExpression, property.SetMethod, new[] { propertyValueExpression });

            return(setterExpression);
        }
Exemple #2
0
        private static ServiceIdentity GetServiceIdentity(Type serviceType, string referenceName, ConstructionFactorySettings settings)
        {
            // First try to get a specific identity based on the parameter name.
            if (settings.UsePrefixResolution)
            {
                var identity = serviceType.TryGetServiceIdentity(referenceName);
                if (identity != null)
                {
                    return(identity);
                }
            }

            // Use default identity if no specific could be created based on parameter name.
            return(new ServiceIdentity(serviceType));
        }
Exemple #3
0
        private static IActivator TryGetActivator(Type serviceType, string referenceName, IContainer container, ConstructionFactorySettings settings)
        {
            var typeInfo = serviceType.GetTypeInfo();

            if (typeInfo.IsValueType)
            {
                return(null);
            }

            var serviceIdentity = GetServiceIdentity(serviceType, referenceName, settings);
            var activator       = container.GetActivator(serviceIdentity);

            if (activator == null)
            {
                return(null);
            }

            return(activator);
        }
Exemple #4
0
        private static IActivator[] TryGetParameterActivators(ParameterInfo[] parameters, IContainer container, ConstructionFactorySettings settings)
        {
            var activators = new IActivator[parameters.Length];

            for (var i = 0; i < parameters.Length; i++)
            {
                var parameter = parameters[i];
                var activator = TryGetActivator(parameter.ParameterType, parameter.Name, container, settings);
                if (activator == null)
                {
                    return(null);
                }

                activators[i] = activator;
            }

            return(activators);
        }
Exemple #5
0
        private static ConstructorDetails TryGetConstructorDetails(TypeInfo typeInfo, IContainer container, ConstructionFactorySettings settings)
        {
            var candidates =
                from constructor in typeInfo.DeclaredConstructors
                where constructor.IsStatic == false
                let parameters = constructor.GetParameters()
                                 orderby parameters.Length descending
                                 let activators = TryGetParameterActivators(parameters, container, settings)
                                                  where activators != null
                                                  select new ConstructorDetails
            {
                ConstructorInfo = constructor,
                Parameters      = parameters,
                Activators      = activators
            };

            return(candidates.FirstOrDefault());
        }