Esempio n. 1
0
        public void ExceptionInPropertySetterIsThrownProperly()
        {
            PropertyInfo property =
                typeof(ThrowsExceptionFromInjectedProperty).GetProperty("Foo", BindingFlags.Public | BindingFlags.Instance);
            IPropertyInjector injector = Factory.GetInjector(property);

            Assert.That(injector, Is.Not.Null);

            ThrowsExceptionFromInjectedProperty mock = new ThrowsExceptionFromInjectedProperty();

            injector.Set(mock, null);
        }
Esempio n. 2
0
        public void PropertyInjectorCanSetValueType()
        {
            PropertyInfo property =
                typeof(PropertyAndFieldInvocationObject).GetProperty("Value", BindingFlags.Public | BindingFlags.Instance);
            IPropertyInjector injector = Factory.GetInjector(property);

            Assert.That(injector, Is.Not.Null);

            PropertyAndFieldInvocationObject mock = new PropertyAndFieldInvocationObject();

            injector.Set(mock, 42);

            Assert.That(mock.Value, Is.EqualTo(42));
        }
Esempio n. 3
0
        public void PropertyInjectorCanSetReferenceType()
        {
            PropertyInfo property =
                typeof(PropertyAndFieldInvocationObject).GetProperty("Message", BindingFlags.Public | BindingFlags.Instance);
            IPropertyInjector injector = Factory.GetInjector(property);

            Assert.That(injector, Is.Not.Null);

            PropertyAndFieldInvocationObject mock = new PropertyAndFieldInvocationObject();

            injector.Set(mock, "Hello, world!");

            Assert.That(mock.Message, Is.EqualTo("Hello, world!"));
        }
        /*----------------------------------------------------------------------------------------*/
        /// <summary>
        /// Executed when the instance is being initialized.
        /// </summary>
        /// <param name="context">The activation context.</param>
        /// <returns>A value indicating whether to proceed or stop the execution of the strategy chain.</returns>
        public override StrategyResult Initialize(IContext context)
        {
            IList <PropertyInjectionDirective> directives = context.Plan.Directives.GetAll <PropertyInjectionDirective>();

            if (directives.Count > 0)
            {
                var contextFactory  = context.Binding.Components.ContextFactory;
                var injectorFactory = context.Binding.Components.InjectorFactory;
                var converter       = context.Binding.Components.Converter;

                foreach (PropertyInjectionDirective directive in directives)
                {
                    // First, try to get the value from a context parameter.
                    object value = context.Parameters.GetValueOf <PropertyValueParameter>(directive.Target.Name, context);

                    // Next, try to get the value from a binding parameter.
                    if (value == null)
                    {
                        value = context.Binding.Parameters.GetValueOf <PropertyValueParameter>(directive.Target.Name, context);
                    }

                    // If no overrides have been declared, activate a service of the proper type to use as the value.
                    if (value == null)
                    {
                        // Create a new context in which the property's value will be activated.
                        IContext injectionContext = contextFactory.CreateChild(context,
                                                                               directive.Member, directive.Target, directive.Argument.Optional);

                        // Resolve the value to inject into the property.
                        value = directive.Argument.Resolver.Resolve(context, injectionContext);
                    }

                    // Convert the value if necessary.
                    if (!converter.Convert(value, directive.Target.Type, out value))
                    {
                        throw new ActivationException(ExceptionFormatter.CouldNotConvertValueForInjection(context, directive.Target, value));
                    }

                    // Get an injector that can set the property's value.
                    IPropertyInjector injector = injectorFactory.GetInjector(directive.Member);

                    // Inject the value.
                    injector.Set(context.Instance, value);
                }
            }

            return(StrategyResult.Proceed);
        }