Created
November 21, 2023 10:28
-
-
Save VictorKoenders/84b83ef0ce0722f23103ba4ffde8f67b to your computer and use it in GitHub Desktop.
Dump C# provider info
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var configuration = //... ; | |
foreach (IConfigurationProvider provider in configuration.Providers) | |
{ | |
DumpProviderSource(dbg, "", provider); | |
} | |
// | |
private void DumpProviderSource(StringBuilder dbg, string indent, IConfigurationProvider provider) | |
{ | |
dbg.AppendLine(indent + provider.GetType().FullName + ":"); | |
switch (provider) | |
{ | |
case ChainedConfigurationProvider chainedConfigurationProvider: | |
var config = typeof(ChainedConfigurationProvider).GetField("_config", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(chainedConfigurationProvider); | |
switch (config) | |
{ | |
case ConfigurationRoot configurationRoot: | |
foreach (var childProvider in configurationRoot.Providers) | |
{ | |
DumpProviderSource(dbg, indent + " ", childProvider); | |
} | |
break; | |
case ConfigurationSection configurationSection: | |
dbg.AppendLine(indent + " " + configurationSection.Key + " = " + configurationSection.Value); | |
break; | |
default: | |
break; | |
} | |
break; | |
case MemoryConfigurationProvider memoryConfigurationProvider: | |
foreach (var kv in memoryConfigurationProvider) | |
{ | |
dbg.AppendLine(indent + " " + kv.Key + " = " + kv.Value); | |
} | |
break; | |
// handled by ConfigurationProvider | |
// case JsonConfigurationProvider jsonConfigurationProvider: | |
// break; | |
// handled by ConfigurationProvider | |
// case Microsoft.Extensions.Configuration.EnvironmentVariables.EnvironmentVariablesConfigurationProvider environmentVariablesConfigurationProvider: | |
// break; | |
case ConfigurationProvider configurationProvider: | |
var data = (IDictionary<string, string>)typeof(ConfigurationProvider).GetProperty("Data", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(configurationProvider); | |
foreach (var kv in data) | |
{ | |
dbg.AppendLine(indent + " " + kv.Key + " = " + kv.Value); | |
} | |
break; | |
default: | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment