private Expression WrapWithPropertyInjectorInternal(
            Type implementationType, Expression expressionToWrap)
        {
            PropertyInfo[] properties = this.GetPropertiesToInject(implementationType);

            if (properties.Length > 0)
            {
                PropertyInjectionHelper.VerifyProperties(properties);

                var data = PropertyInjectionHelper.BuildPropertyInjectionExpression(
                    this.Container, implementationType, properties, expressionToWrap);

                expressionToWrap = data.Expression;

                var knownRelationships =
                    from pair in data.Producers.Zip(data.Properties, (prod, prop) => new { prod, prop })
                    select new KnownRelationship(
                        implementationType: implementationType,
                        lifestyle: this.Lifestyle,
                        consumer: new InjectionConsumerInfo(implementationType, pair.prop !),
                        dependency: pair.prod !);

                foreach (var knownRelationship in knownRelationships)
                {
                    this.AddRelationship(knownRelationship);
                }
            }

            return(expressionToWrap);
        }
        private PropertyInfo[] GetPropertiesToInject(Type implementationType)
        {
            var propertySelector = this.Container.Options.PropertySelectionBehavior;

            var candidates = PropertyInjectionHelper.GetCandidateInjectionPropertiesFor(implementationType);

            // Optimization: Safes creation of multiple objects in case there are no candidates.
            return(candidates.Length == 0
                ? candidates
                : candidates.Where(p => propertySelector.SelectProperty(implementationType, p)).ToArray());
        }
Example #3
0
        private PropertyInfo[] GetPropertiesToInject(Type serviceType, Type implementationType)
        {
            var propertySelector = this.Container.Options.PropertySelectionBehavior;

            var candidates = PropertyInjectionHelper.GetCandidateInjectionPropertiesFor(implementationType);

            return((
                       from property in candidates
                       where serviceType != typeof(Container)
                       where propertySelector.SelectProperty(serviceType, property)
                       select property)
                   .ToArray());
        }
Example #4
0
        private Expression WrapWithPropertyInjectorInternal(Type serviceType, Type implementationType,
                                                            Expression expression)
        {
            var properties = this.GetPropertiesToInject(serviceType, implementationType);

            if (properties.Any())
            {
                PropertyInjectionHelper.VerifyProperties(properties);

                expression = PropertyInjectionHelper.BuildPropertyInjectionExpression(
                    this.Container, serviceType, implementationType, properties, expression);

                this.AddPropertiesAsKnownRelationships(serviceType, implementationType, properties);
            }

            return(expression);
        }
        private Func <object, object> BuildDelegate()
        {
            PropertyInfo[] propertiesToInject = this.GetInjectableProperties();

            var dummyExpression = Expression.Constant(null, this.Type);

            var propertyInjectionExpression = PropertyInjectionHelper.BuildPropertyInjectionExpression(
                this.container, this.Type, propertiesToInject, dummyExpression);

            var parameter = Expression.Parameter(typeof(object));

            propertyInjectionExpression = SubExpressionReplacer.Replace(
                expressionToAlter: propertyInjectionExpression,
                subExpressionToFind: dummyExpression,
                replacementExpression: Expression.Convert(parameter, this.Type));

            return(Expression.Lambda <Func <object, object> >(propertyInjectionExpression, parameter).Compile());
        }
Example #6
0
        private Expression WrapWithPropertyInjectorInternal(Type implementationType,
                                                            Expression expressionToWrap)
        {
            PropertyInfo[] properties = this.GetPropertiesToInject(implementationType);

            if (properties.Any())
            {
                PropertyInjectionHelper.VerifyProperties(properties);

                var data = PropertyInjectionHelper.BuildPropertyInjectionExpression(
                    this.Container, implementationType, properties, expressionToWrap);

                expressionToWrap = data.Expression;

                this.AddRelationships(implementationType, data.Producers);
            }

            return(expressionToWrap);
        }