我正在寻找重定向standard.Net ConfigurationManager 类到另一个文件;entirely。该路径是在运行时确定的,因此我无法使用 configSource或类似的问题(这不是重复的问题 - 我已经看过其他问题)。
我本质上是在尝试复制 ASP.Net 在幕后所做的事情。因此,不仅我的类应该从新的配置文件中读取,还应该读取任何标准的 .Net 内容(我特别尝试使用的内容是 system.codeDom 元素)。
我已经破解了 Reflector 并开始研究 ASP.Net 是如何实现的 - 它非常复杂并且完全没有文档记录。我希望其他人已经对这个过程进行了逆向工程。不一定要寻找完整的解决方案(会很好),而只是文档.
我终于弄明白了。有一个公共记录在案意味着这样做 - 但它隐藏在 .Net 框架的深处。更改您自己的配置文件需要反射(只需刷新 ConfigurationManager 即可);但可以更改通过公共 API 创建的 AppDomain 的配置文件。
不用感谢我提交的 Microsoft Connect 功能,这里是代码:
class Program
{
static void Main(string[] args)
{
// Setup information for the new appdomain.
AppDomainSetup setup = new AppDomainSetup();
setup.ConfigurationFile = "C:\\my.config";
// Create the new appdomain with the new config.
AppDomain d2 = AppDomain.CreateDomain("customDomain", AppDomain.CurrentDomain.Evidence, setup);
// Call the write config method in that appdomain.
CrossAppDomainDelegate del = new CrossAppDomainDelegate(WriteConfig);
d2.DoCallBack(del);
// Call the write config in our appdomain.
WriteConfig();
Console.ReadLine();
}
static void WriteConfig()
{
// Get our config file.
Configuration c = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Write it out.
Console.WriteLine("{0}: {1}", AppDomain.CurrentDomain.FriendlyName, c.FilePath);
}
}
Output:
customDomain: C:\my.config
InternalConfigTest.vshost.exe: D:\Profile\...\InternalConfigTest.vshost.exe.config
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)