Skip to content

Commit e72f1ea

Browse files
Try using the current configuration before falling back to ConfigManager when looking for section handler definitions. (#210)
1 parent 01ddfc1 commit e72f1ea

1 file changed

Lines changed: 33 additions & 21 deletions

File tree

src/Base/SectionHandlerSection.cs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,50 @@ internal static ISectionHandler GetSectionHandler<T>(T configSection) where T :
4444
if (configSection == null)
4545
return null;
4646

47+
SectionHandlersSection handlerSection = GetSectionHandlersSection(configSection);
4748

48-
if (!(ConfigurationManager.GetSection(handlerSectionName) is SectionHandlersSection handlerSection))
49+
if (handlerSection != null)
4950
{
50-
handlerSection = new SectionHandlersSection();
51-
handlerSection.InitializeDefault();
52-
}
53-
54-
// Look at each handler to see if it works on this section. Reverse order so last match wins.
55-
// .IsSubclassOf() requires an exact type match. So SectionHandler<BaseConfigSectionType> won't work.
56-
Type sectionHandlerGenericTemplate = typeof(SectionHandler<>);
57-
Type sectionHandlerDesiredType = sectionHandlerGenericTemplate.MakeGenericType(configSection.GetType());
58-
for (int i = handlerSection.Handlers.Count; i-- > 0; )
59-
{
60-
Type handlerType = Type.GetType(handlerSection.Handlers[i].Type);
61-
if (handlerType != null && handlerType.IsSubclassOf(sectionHandlerDesiredType))
51+
// Look at each handler to see if it works on this section. Reverse order so last match wins.
52+
// .IsSubclassOf() requires an exact type match. So SectionHandler<BaseConfigSectionType> won't work.
53+
Type sectionHandlerGenericTemplate = typeof(SectionHandler<>);
54+
Type sectionHandlerDesiredType = sectionHandlerGenericTemplate.MakeGenericType(configSection.GetType());
55+
for (int i = handlerSection.Handlers.Count; i-- > 0;)
6256
{
63-
if (Activator.CreateInstance(handlerType) is ISectionHandler handler)
57+
Type handlerType = Type.GetType(handlerSection.Handlers[i].Type);
58+
if (handlerType != null && handlerType.IsSubclassOf(sectionHandlerDesiredType))
6459
{
65-
ProviderSettings settings = handlerSection.Handlers[i];
66-
NameValueCollection clonedParams = new NameValueCollection(settings.Parameters.Count);
67-
foreach (string key in settings.Parameters)
68-
clonedParams[key] = settings.Parameters[key];
60+
if (Activator.CreateInstance(handlerType) is ISectionHandler handler)
61+
{
62+
ProviderSettings settings = handlerSection.Handlers[i];
63+
NameValueCollection clonedParams = new NameValueCollection(settings.Parameters.Count);
64+
foreach (string key in settings.Parameters)
65+
clonedParams[key] = settings.Parameters[key];
6966

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

73-
return handler;
70+
return handler;
71+
}
7472
}
7573
}
7674
}
7775

7876
throw new Exception($"Error in Configuration: Cannot find ISectionHandler for '{configSection.SectionInformation.Name}' section.");
7977
}
78+
79+
private static SectionHandlersSection GetSectionHandlersSection(ConfigurationSection currentSection)
80+
{
81+
SectionHandlersSection handlersSection = (currentSection?.CurrentConfiguration?.GetSection(handlerSectionName) as SectionHandlersSection)
82+
?? (ConfigurationManager.GetSection(handlerSectionName) as SectionHandlersSection);
83+
84+
if (handlersSection == null)
85+
{
86+
handlersSection = new SectionHandlersSection();
87+
handlersSection.InitializeDefault();
88+
}
89+
90+
return handlersSection;
91+
}
8092
}
8193
}

0 commit comments

Comments
 (0)