Beispiel #1
0
        /// <summary>
        /// Override of <see cref="IBuilderStrategy.BuildUp"/>. Updates the instance name using a name mapper associated to type <paramref name="t"/>
        /// so later strategies in the build chain will use the updated instance name.
        /// </summary>
        /// <remarks>
        /// Will only update the instance name if it is <see langword="null"/>.
        /// </remarks>
        /// <param name="context">The <see cref="IBuilderContext"/> that represents the current building process.</param>
        /// <param name="t">The type to build.</param>
        /// <param name="existing">The existing object. Should be <see langword="null"/>.</param>
        /// <param name="id">The ID of the object to be created.</param>
        /// <returns>The created object.</returns>
        /// <exception cref="System.Configuration.ConfigurationErrorsException"> when the configuration required to do the mapping is not present or is
        /// invalid in the configuration source.</exception>
        public override object BuildUp(IBuilderContext context, Type t, object existing, string id)
        {
            if (id == null)
            {
                ConfigurationReflectionCache reflectionCache = GetReflectionCache(context);

                IConfigurationNameMapper mapper = reflectionCache.GetConfigurationNameMapper(t);
                if (mapper != null)
                {
                    id = mapper.MapName(id, GetConfigurationSource(context));
                }
            }

            return(base.BuildUp(context, t, existing, id));
        }
        public override void PreBuildUp(IBuilderContext context)
        {
            base.PreBuildUp(context);
            NamedTypeBuildKey key = (NamedTypeBuildKey)context.BuildKey;

            if (key.Name == null)
            {
                ConfigurationReflectionCache reflectionCache = GetReflectionCache(context);
                IConfigurationNameMapper     mapper          = reflectionCache.GetConfigurationNameMapper(key.Type);
                if (mapper != null)
                {
                    context.BuildKey = new NamedTypeBuildKey(key.Type, mapper.MapName(null, GetConfigurationSource(context)));
                }
            }
        }