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.");
        }
コード例 #2
0
        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);
        }