有没有办法得到一个系统。配置。配置的实例的基础上任意xml?
-
09-06-2019 - |
题
我想单元测试一个自定义ConfigurationSection我写的,我想到负荷的一些任意配置XML成 系统。配置。配置 对每个试验(而不是把测试配置xml在测试。dll。配置文件。这就是我想要做这样的事情:
Configuration testConfig = new Configuration("<?xml version=\"1.0\"?><configuration>...</configuration>");
MyCustomConfigSection section = testConfig.GetSection("mycustomconfigsection");
Assert.That(section != null);
然而,它看起来像 ConfigurationManager 只会给你的配置情况,相关文件或机的配置。有没有办法负载任意XML入的配置实例?
解决方案
其实是有一种方法,我已经发现了....
你需要定义一个新的类继承你的原始构成部分如下:
public class MyXmlCustomConfigSection : MyCustomConfigSection
{
public MyXmlCustomConfigSection (string configXml)
{
XmlTextReader reader = new XmlTextReader(new StringReader(configXml));
DeserializeSection(reader);
}
}
然后你可以化你的ConfigurationSection对象如下:
string configXml = "<?xml version=\"1.0\"?><configuration>...</configuration>";
MyCustomConfigSection config = new MyXmlCustomConfigSection(configXml);
希望这可以帮助别人:-)
其他提示
我觉得你在找什么是ConfigurationManager.OpenMappedExeConfiguration
它允许你打开的配置文件的指定用一个文件路径(包裹在里面了 ExeConfigurationFileMap)
如果有什么其他的海报说的是真的,你不希望创建一个全新的XML文件进行测试,然后,我会建议你把你的配置的编辑在测试方法本身,然后运行测试对新改变结构的数据。
看类的成员,我说答案很可能是没有*.我不知道你为什么要这样做,无论如何,而不是创建你自己的配置文件。
*那不是,不包括混乱的反黑客
不隶属于 StackOverflow