/*----------------------------------------------------------------------------------------*/
		/// <summary>
		/// Creates an injection directive for the specified property.
		/// </summary>
		/// <param name="binding">The binding.</param>
		/// <param name="property">The property to create the directive for.</param>
		/// <returns>The created directive.</returns>
		public PropertyInjectionDirective Create(IBinding binding, PropertyInfo property)
		{
			var directive = new PropertyInjectionDirective(property);
			directive.Argument = CreateArgument(binding, new PropertyTarget(property));

			return directive;
		}
        /*----------------------------------------------------------------------------------------*/
        /// <summary>
        /// Creates an injection directive for the specified property.
        /// </summary>
        /// <param name="binding">The binding.</param>
        /// <param name="property">The property to create the directive for.</param>
        /// <returns>The created directive.</returns>
        public PropertyInjectionDirective Create(IBinding binding, PropertyInfo property)
        {
            var directive = new PropertyInjectionDirective(property);

            directive.Argument = CreateArgument(binding, new PropertyTarget(property));

            return(directive);
        }
        public void CreatesTargetForProperty()
        {
            var method = typeof(Dummy).GetProperty("Foo");
            PropertyInjector injector = delegate { };

            directive = new PropertyInjectionDirective(method, injector);

            directive.Target.Name.ShouldBe("Foo");
            directive.Target.Type.ShouldBe(typeof(int));
        }
Beispiel #4
0
        public PlanTests()
        {
            _constructor1 = CreateConstructorInjectionDirective();
            _constructor2 = CreateConstructorInjectionDirective();
            _property     = CreatePropertyInjectionDirective();

            _plan = new Plan(this.GetType());
            _plan.Add(_constructor1);
            _plan.Add(_property);
            _plan.Add(_constructor2);
        }
Beispiel #5
0
        public void CreatesTargetForProperty()
        {
#if !WINRT
            var method = typeof(Dummy).GetProperty("Foo");
#else
            var method = typeof(Dummy).GetRuntimeProperty("Foo");
#endif
            PropertyInjector injector = delegate { };

            directive = new PropertyInjectionDirective(method, injector);

            directive.Target.Name.Should().Be("Foo");
            directive.Target.Type.Should().Be(typeof(int));
        }
        /// <summary>
        /// Applies user supplied override values to instance properties.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="reference">A reference to the instance being activated.</param>
        /// <param name="propertyValues">The parameter override value accessors.</param>
        /// <exception cref="ActivationException">A given <see cref="IPropertyValue"/> cannot be resolved to a property of the specified instance.</exception>
        private void AssignPropertyOverrides(IContext context, InstanceReference reference, List <IPropertyValue> propertyValues)
        {
            var properties = reference.Instance.GetType().GetProperties(this.Flags);

            foreach (var propertyValue in propertyValues)
            {
                var propertyInfo = FindPropertyByName(properties, propertyValue.Name, StringComparison.Ordinal);

                if (propertyInfo == null)
                {
                    throw new ActivationException(this.exceptionFormatter.CouldNotResolvePropertyForValueInjection(context.Request, propertyValue.Name));
                }

                var target = new PropertyInjectionDirective(propertyInfo, this.injectorFactory.Create(propertyInfo));
                var value  = propertyValue.GetValue(context, target.Target);
                target.Injector(reference.Instance, value);
            }
        }
Beispiel #7
0
        /// <summary>
        /// Applies user supplied override values to instance properties.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="reference">A reference to the instance being activated.</param>
        /// <param name="propertyValues">The parameter override value accessors.</param>
        private void AssignPropertyOverrides(IContext context, InstanceReference reference, IList <IPropertyValue> propertyValues)
        {
            var properties = reference.Instance.GetType().GetRuntimeProperties().FilterPublic(Settings.InjectNonPublic);

            foreach (var propertyValue in propertyValues)
            {
                string propertyName = propertyValue.Name;
                var    propertyInfo = properties.FirstOrDefault(property => string.Equals(property.Name, propertyName, StringComparison.Ordinal));

                if (propertyInfo == null)
                {
                    throw new ActivationException(ExceptionFormatter.CouldNotResolvePropertyForValueInjection(context.Request, propertyName));
                }

                var    target = new PropertyInjectionDirective(propertyInfo, this.InjectorFactory.Create(propertyInfo));
                object value  = this.GetValue(context, target.Target, propertyValues);
                target.Injector(reference.Instance, value);
            }
        }
        /// <summary>
        /// Applies user supplied override values to instance properties.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="reference">A reference to the instance being activated.</param>
        /// <param name="propertyValues">The parameter ovverride value accessors.</param>
        private void AssignProperyOverrides(IContext context, InstanceReference reference, IEnumerable <IParameter> propertyValues)
        {
            var properties = reference.Instance.GetType().GetProperties(Flags);

            foreach (var propertyValue in propertyValues)
            {
                string propertyName = propertyValue.Name;
                var    propertyInfo = properties
                                      .Where(property => string.Equals(property.Name, propertyName, StringComparison.Ordinal))
                                      .FirstOrDefault();

                if (propertyInfo == null)
                {
                    throw new ActivationException(ExceptionFormatter.CouldNotResolveProperyForValueInjection(context.Request, propertyName));
                }

                var    target = new PropertyInjectionDirective(propertyInfo, InjectorFactory.Create(propertyInfo));
                object value  = GetValue(context, target.Target);
                target.Injector(reference.Instance, value);
            }
        }
        /// <summary>
        /// Injects values into the properties as described by <see cref="PropertyInjectionDirective"/>s
        /// contained in the plan.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="reference">A reference to the instance being activated.</param>
        public override void Activate(IContext context, InstanceReference reference)
        {
            Ensure.ArgumentNotNull(context, "context");
            Ensure.ArgumentNotNull(reference, "reference");

            var propertyValues = context.Parameters.Where(parameter => parameter is PropertyValue);
            IEnumerable <string> parameterNames = propertyValues.Select(parameter => parameter.Name);

            foreach (var directive in context.Plan.GetAll <PropertyInjectionDirective>())
            {
                PropertyInjectionDirective propertyInjectionDirective = directive;
                if (parameterNames.Any(name => string.Equals(name, propertyInjectionDirective)))
                {
                    continue;
                }

                object value = GetValue(context, directive.Target);
                directive.Injector(reference.Instance, value);
            }

            AssignProperyOverrides(context, reference, propertyValues);
        }
Beispiel #10
0
        /*----------------------------------------------------------------------------------------*/
        /// <summary>
        /// Adds an injection directive related to the specified member to the specified activation plan.
        /// </summary>
        /// <param name="binding">The binding that points at the type being inspected.</param>
        /// <param name="type">The type that is being inspected.</param>
        /// <param name="plan">The activation plan to add the directive to.</param>
        /// <param name="member">The member to create a directive for.</param>
        protected override void AddInjectionDirective(IBinding binding, Type type, IActivationPlan plan, PropertyInfo member)
        {
            PropertyInjectionDirective directive = binding.Components.DirectiveFactory.Create(binding, member);

            plan.Directives.Add(directive);
        }