Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A class for IronRuby users that will point the current AppDomain's configuration to a new configuration file.
require 'System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
class ConfigurationSettingsHackery
def set_config_file(new_config_file)
set_config_file_on_current_app_domain(new_config_file)
refresh_current_client_config_paths
reset_configuration_manager_to_force_reload
end
private
def non_public_binding
System::Reflection::BindingFlags.NonPublic
end
def non_public_static_binding
non_public_binding | System::Reflection::BindingFlags.Static
end
def non_public_instance_binding
non_public_binding | System::Reflection::BindingFlags.Instance
end
def set_config_file_on_current_app_domain(new_config_file)
domainSetup = System::AppDomain.CurrentDomain.GetType().
GetProperty("FusionStore", non_public_instance_binding).
GetGetMethod(true).Invoke(System::AppDomain.CurrentDomain, nil)
domainSetup.ConfigurationFile = new_config_file
end
def refresh_current_client_config_paths
configSystem = System::Configuration::ConfigurationManager.
to_clr_type.GetField("s_configSystem", non_public_static_binding).
GetValue(nil)
configHost = configSystem.GetType().
GetField("_configHost", non_public_instance_binding).
GetValue(configSystem)
configHost.GetType().Assembly.
GetType('System.Configuration.ClientConfigPaths').
GetMethod("RefreshCurrent", non_public_static_binding).
Invoke(nil, nil)
end
def reset_configuration_manager_to_force_reload
initStateType = System::Configuration::ConfigurationManager.
to_clr_type.GetNestedType("InitState", non_public_binding)
state = initStateType.GetField("NotStarted").
GetValue(initStateType)
System::Configuration::ConfigurationManager.
to_clr_type.GetField("s_initState", non_public_static_binding).
SetValue(nil, state)
end
end
ConfigurationSettingsHackery.new.set_config_file('c:\\app.config')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment