public override ConfigurationSection ProcessConfigurationSection(ConfigurationSection configSection)
        {
            // Expand mode only works on the raw string input
            if (Mode == KeyValueMode.Expand)
            {
                return(configSection);
            }

            // See if we know how to process this section
            ISectionHandler handler = SectionHandlersSection.GetSectionHandler(configSection);

            if (handler == null)
            {
                return(configSection);
            }

            if (configSection.SectionInformation?.SectionName == "appSettings")
            {
                _appSettings = configSection as AppSettingsSection;
            }

            // Strict Mode. Only replace existing key/values.
            if (Mode == KeyValueMode.Strict)
            {
                foreach (var configItem in handler)
                {
                    // Presumably, UpdateKey will preserve casing appropriately, so newKey is cased as expected.
                    string newKey   = UpdateKey(configItem.Key);
                    string newValue = GetValueInternal(configItem.Key);

                    if (newValue != null)
                    {
                        handler.InsertOrUpdate(newKey, newValue, configItem.Key, configItem.Value);
                    }
                }
            }

            // Greedy Mode. Insert all key/values.
            else if (Mode == KeyValueMode.Greedy)
            {
                EnsureGreedyInitialized();
                foreach (KeyValuePair <string, string> kvp in _cachedValues)
                {
                    if (kvp.Value != null)
                    {
                        // Here, kvp.Key is not from the config file, so it might not be correctly cased. Get the correct casing for UpdateKey.
                        string oldKey = TrimPrefix(kvp.Key);
                        string newKey = UpdateKey(handler.TryGetOriginalCase(oldKey));
                        handler.InsertOrUpdate(newKey, kvp.Value, oldKey);
                    }
                }
            }

            return(configSection);
        }
예제 #2
0
        public override ConfigurationSection ProcessConfigurationSection(ConfigurationSection configSection)
        {
            // Expand mode only works on the raw string input
            if (Mode == KeyValueMode.Expand)
            {
                return(configSection);
            }

            // See if we know how to process this section
            ISectionHandler handler = SectionHandlersSection.GetSectionHandler(configSection);

            if (handler == null)
            {
                return(configSection);
            }

            _appSettings = configSection as AppSettingsSection;

            // Strict Mode. Only replace existing key/values.
            if (Mode == KeyValueMode.Strict)
            {
                foreach (var configItem in handler)
                {
                    string newValue = GetValueInternal(configItem.Key);
                    string newKey   = UpdateKey(configItem.Key);

                    if (newValue != null)
                    {
                        handler.InsertOrUpdate(newKey, newValue, configItem.Key, configItem.Value);
                    }
                }
            }

            // Greedy Mode. Insert all key/values.
            else if (Mode == KeyValueMode.Greedy)
            {
                EnsureGreedyInitialized();
                foreach (KeyValuePair <string, string> kvp in _cachedValues)
                {
                    if (kvp.Value != null)
                    {
                        string oldKey = TrimPrefix(kvp.Key);
                        string newKey = UpdateKey(oldKey);
                        handler.InsertOrUpdate(newKey, kvp.Value, oldKey);
                    }
                }
            }

            return(configSection);
        }
        static internal ISectionHandler GetSectionHandler <T>(T configSection) where T : ConfigurationSection
        {
            if (configSection == null)
            {
                return(null);
            }

            SectionHandlersSection handlerSection = ConfigurationManager.GetSection(handlerSectionName) as SectionHandlersSection;

            if (handlerSection == null)
            {
                handlerSection = new SectionHandlersSection();
                handlerSection.InitializeDefault();
            }

            // Look at each handler to see if it works on this section. Reverse order so last match wins.
            // .IsSubclassOf() requires an exact type match. So SectionHandler<BaseConfigSectionType> won't work.
            Type sectionHandlerGenericTemplate = typeof(SectionHandler <>);
            Type sectionHandlerDesiredType     = sectionHandlerGenericTemplate.MakeGenericType(configSection.GetType());

            for (int i = handlerSection.Handlers.Count; i-- > 0;)
            {
                Type handlerType = Type.GetType(handlerSection.Handlers[i].Type);
                if (handlerType != null && handlerType.IsSubclassOf(sectionHandlerDesiredType))
                {
                    ISectionHandler handler = Activator.CreateInstance(handlerType) as ISectionHandler;
                    if (handler != null)
                    {
                        ProviderSettings    settings     = handlerSection.Handlers[i];
                        NameValueCollection clonedParams = new NameValueCollection(settings.Parameters.Count);
                        foreach (string key in settings.Parameters)
                        {
                            clonedParams[key] = settings.Parameters[key];
                        }

                        MethodInfo init = sectionHandlerDesiredType.GetMethod("Initialize", BindingFlags.NonPublic | BindingFlags.Instance);
                        init.Invoke(handler, new object[] { settings.Name, configSection, clonedParams });

                        return(handler);
                    }
                }
            }

            throw new Exception($"Error in Configuration: Cannot find ISectionHandler for '{configSection.SectionInformation.Name}' section.");
        }
        static internal ISectionHandler GetSectionHandler <T>(T configSection) where T : ConfigurationSection
        {
            if (configSection == null)
            {
                return(null);
            }

            SectionHandlersSection handlerSection = ConfigurationManager.GetSection(handlerSectionName) as SectionHandlersSection;

            if (handlerSection == null)
            {
                handlerSection = new SectionHandlersSection();
                handlerSection.InitializeDefault();
            }

            // Look at each handler to see if it works on this section. Reverse order so last match wins.
            // .IsSubclassOf() requires an exact type match. So SectionHandler<BaseConfigSectionType> won't work.
            Type sectionHandlerGenericTemplate = typeof(SectionHandler <>);
            Type sectionHandlerDesiredType     = sectionHandlerGenericTemplate.MakeGenericType(configSection.GetType());

            for (int i = handlerSection.Handlers.Count; i-- > 0;)
            {
                Type handlerType = Type.GetType(handlerSection.Handlers[i].Type);
                if (handlerType != null && handlerType.IsSubclassOf(sectionHandlerDesiredType))
                {
                    ISectionHandler handler = Activator.CreateInstance(handlerType) as ISectionHandler;
                    if (handler != null)
                    {
                        sectionHandlerDesiredType.GetProperty("ConfigSection").SetValue(handler, configSection);
                    }
                    return(handler);
                }
            }

            return(null);
        }