Something that may be useful to you, I created it a while ago.
This is an extension method that you can use to force the configuration to reload from a specific file. It uses reflection to change private fields in the manager, clears the configuration, and then conditionally reloads it. This is much easier than manually editing the LINQPad configuration file.
public static void ForceNewConfigFile(this Type type, bool initialize = true) { var path = type.Assembly.Location + ".config"; if (!File.Exists(path)) throw new Exception("Cannot find file " + path + "."); AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", path); var typeOfConfigManager = typeof(ConfigurationManager); typeOfConfigManager.GetField("s_initState", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, 0); typeOfConfigManager.GetField("s_configSystem", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, null); var typeOfClientConfigPaths = typeOfConfigManager.Assembly.GetTypes().Where(x => x.FullName == "System.Configuration.ClientConfigPaths").Single(); typeOfClientConfigPaths.GetField("s_current", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, null); if (initialize) { var dummy = ConfigurationManager.AppSettings; } }
Usage example:
typeof(SomeType).ForceNewConfigFile(); System.Configuration.ConfigurationManager.AppSettings.Dump();
SomeType is simply the type contained in the assembly that will be used as the source for the location of the configuration file. It is assumed: the configuration file exists next to the DLL file and is called {Assembly.Location}.config .
source share