Exemple #1
0
        private object DoBuildUp(Type t, object existing, string name, IEnumerable <ResolverOverride> resolverOverrides)
        {
            IBuilderContext context = null;

            try
            {
                context =
                    new BuilderContext(
                        GetStrategies(),
                        lifetimeContainer,
                        policies,
                        new NamedTypeBuildKey(t, name),
                        existing);
                context.AddResolverOverrides(resolverOverrides);

                if (t.IsGenericTypeDefinition)
                {
                    throw new ArgumentException(
                              string.Format(CultureInfo.CurrentCulture,
                                            Resources.CannotResolveOpenGenericType,
                                            t.FullName), "t");
                }

                return(context.Strategies.ExecuteBuildUp(context));
            }
            catch (Exception ex)
            {
                throw new ResolutionFailedException(t, name, ex, context);
            }
        }
        /// <summary>
        /// Run an existing object through the container and perform injection on it.
        /// </summary>
        /// <remarks>
        /// <para>
        /// This method is useful when you don'type control the construction of an
        /// instance (ASP.NET pages or objects created via XAML, for instance)
        /// but you still want properties and other injection performed.
        /// </para></remarks>
        /// <param name="typeToBuild"><see cref="Type"/> of object to perform injection on.</param>
        /// <param name="existing">Instance to build up.</param>
        /// <param name="name">name to use when looking up the typemappings and other configurations.</param>
        /// <param name="resolverOverrides">Any overrides for the buildup.</param>
        /// <returns>The resulting object. By default, this will be <paramref name="existing"/>, but
        /// container extensions may add things like automatic proxy creation which would
        /// cause this to return a different object (but still type compatible with <paramref name="typeToBuild"/>).</returns>
        public object BuildUp(Type typeToBuild, object existing, string name, params ResolverOverride[] resolverOverrides)
        {
            var type = typeToBuild ?? throw new ArgumentNullException(nameof(typeToBuild));

            if (null != existing)
            {
                InstanceIsAssignable(type, existing, nameof(existing));
            }

            var             key     = new NamedTypeBuildKey(type, name);
            IBuilderContext context = null;

            try
            {
                context = new BuilderContext(this, new StrategyChain(_strategies),
                                             _lifetimeContainer,
                                             _context,
                                             key,
                                             existing);

                if (null != resolverOverrides && 0 != resolverOverrides.Length)
                {
                    context.AddResolverOverrides(resolverOverrides);
                }

                if (key.Type.GetTypeInfo().IsGenericTypeDefinition)
                {
                    throw new ArgumentException(
                              string.Format(CultureInfo.CurrentCulture,
                                            Constants.CannotResolveOpenGenericType,
                                            key.Type.FullName), nameof(key.Type));
                }

                context.Strategies.BuildUp(context);
                return(context.Existing);
            }
            catch (Exception ex)
            {
                throw new ResolutionFailedException(key.Type, key.Name, ex, context);
            }
        }
Exemple #3
0
        private object DoBuildUp(Type t, object existing, string name, IEnumerable <ResolverOverride> resolverOverrides)
        {
            IBuilderContext context = null;

            // Ariel
            //if (t.Name == "IDataProtectionProvider") System.Diagnostics.Debugger.Break();
            if (t.Name == "IApplicationDiscriminator")
            {
                System.Diagnostics.Debugger.Break();
            }

            //if (t.Name == "IActionInvokerFactory") System.Diagnostics.Debugger.Break();

            try
            {
                context =
                    new BuilderContext(
                        GetStrategies(),
                        lifetimeContainer,
                        policies,
                        new NamedTypeBuildKey(t, name),
                        existing);
                context.AddResolverOverrides(resolverOverrides);

                if (t.GetTypeInfo().IsGenericTypeDefinition)
                {
                    throw new ArgumentException(
                              string.Format(CultureInfo.CurrentCulture,
                                            Resources.CannotResolveOpenGenericType,
                                            t.FullName), "t");
                }

                return(context.Strategies.ExecuteBuildUp(context));
            }
            catch (Exception ex)
            {
                throw new ResolutionFailedException(t, name, ex, context);
            }
        }