コード例 #1
0
        public void ExceptionInConstructorIsThrownProperly()
        {
            ConstructorInfo constructor = typeof(ThrowsExceptionFromInjectedConstructor).GetConstructor(Type.EmptyTypes);

            IConstructorInjector injector = Factory.GetInjector(constructor);

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

            ThrowsExceptionFromInjectedConstructor mock = injector.Invoke(new object[0]) as ThrowsExceptionFromInjectedConstructor;
        }
コード例 #2
0
        public void ConstructorInjectorCanCreateInstanceOfType()
        {
            ConstructorInfo constructor = typeof(ConstructorInvocationObject).GetConstructor(new Type[] { typeof(int) });

            IConstructorInjector injector = Factory.GetInjector(constructor);

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

            ConstructorInvocationObject mock = injector.Invoke(new object[] { 42 }) as ConstructorInvocationObject;

            Assert.That(mock, Is.Not.Null);
            Assert.That(mock.Value, Is.EqualTo(42));
        }
コード例 #3
0
        /*----------------------------------------------------------------------------------------*/
        #region Public Methods
        /// <summary>
        /// Gets an injector for the specified constructor.
        /// </summary>
        /// <param name="constructor">The constructor that the injector will invoke.</param>
        /// <returns>A new injector for the constructor.</returns>
        public IConstructorInjector GetInjector(ConstructorInfo constructor)
        {
            lock (_constructorInjectors)
            {
                if (_constructorInjectors.ContainsKey(constructor))
                {
                    return(_constructorInjectors[constructor]);
                }

                IConstructorInjector injector = CreateInjector(constructor);
                _constructorInjectors.Add(constructor, injector);

                return(injector);
            }
        }
コード例 #4
0
        /*----------------------------------------------------------------------------------------*/
        #region Protected Methods
        /// <summary>
        /// Calls the injection constructor defined in the context's activation plan, and returns
        /// the resulting object.
        /// </summary>
        /// <param name="context">The context in which the activation is occurring.</param>
        /// <returns>The instance of the type.</returns>
        protected virtual object CallInjectionConstructor(IContext context)
        {
            var directive = context.Plan.Directives.GetOne <ConstructorInjectionDirective>();

            if (directive == null)
            {
                throw new ActivationException(ExceptionFormatter.NoConstructorsAvailable(context));
            }

            // Resolve the dependency markers in the constructor injection directive.
            object[] arguments = ResolveConstructorArguments(context, directive);

            // Get an injector that can call the injection constructor.
            IConstructorInjector injector = context.Binding.Components.InjectorFactory.GetInjector(directive.Member);

            // Call the constructor and return the created object.
            return(injector.Invoke(arguments));
        }