C# 有没有办法基于任意 xml 获取 System.Configuration.Configuration 实例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to get a System.Configuration.Configuration instance based on arbitrary xml?
提问by Matt
I'm trying to unit test a custom ConfigurationSection I've written, and I'd like to load some arbitrary configuration XML into a System.Configuration.Configurationfor each test (rather than put the test configuration xml in the Tests.dll.config file. That is, I'd like to do something like this:
我正在尝试对我编写的自定义 ConfigurationSection 进行单元测试,并且我想将一些任意配置 XML 加载到每个测试的System.Configuration.Configuration中(而不是将测试配置 xml 放在 Tests.dll 中。配置文件。也就是说,我想做这样的事情:
Configuration testConfig = new Configuration("<?xml version=\"1.0\"?><configuration>...</configuration>");
MyCustomConfigSection section = testConfig.GetSection("mycustomconfigsection");
Assert.That(section != null);
However, it looks like ConfigurationManagerwill only give you Configuration instances that are associated with an EXE file or a machine config. Is there a way to load arbitrary XML into a Configuration instance?
但是,看起来ConfigurationManager只会为您提供与 EXE 文件或机器配置相关联的配置实例。有没有办法将任意 XML 加载到 Configuration 实例中?
采纳答案by Oliver Pearmain
There is actually a way I've discovered....
实际上我发现了一种方法......
You need to define a new class inheriting from your original configuration section as follows:
您需要定义一个继承自原始配置部分的新类,如下所示:
public class MyXmlCustomConfigSection : MyCustomConfigSection
{
public MyXmlCustomConfigSection (string configXml)
{
XmlTextReader reader = new XmlTextReader(new StringReader(configXml));
DeserializeSection(reader);
}
}
You can then instantiate your ConfigurationSection object as follows:
然后,您可以按如下方式实例化 ConfigurationSection 对象:
string configXml = "<?xml version=\"1.0\"?><configuration>...</configuration>";
MyCustomConfigSection config = new MyXmlCustomConfigSection(configXml);
Hope it helps someone :-)
希望它可以帮助某人:-)
回答by TheSmurf
Looking at the members of the class, I'd say the answer is probably no*. I'm not sure why you'd want to do this anyway, rather than create your own XML configuration file.
看看班上的成员,我会说答案可能是否定的。我不确定您为什么要这样做,而不是创建自己的 XML 配置文件。
*That's no, excluding messy reflection hacks
*那不是,不包括凌乱的反射黑客
回答by Nicholas
I think what you're looking for is ConfigurationManager.OpenMappedExeConfiguration
我认为您正在寻找的是 ConfigurationManager。OpenMappedExeConfiguration
It allows you to open a configuration file that you specify with a file path (wrapped inside a ExeConfigurationFileMap)
它允许您打开使用文件路径指定的配置文件(包装在ExeConfigurationFileMap 中)
If what the other poster said is true, and you don't wish to create a whole new XML file for testing, then I'd recommend you put your Configuration edits in the Test method itself, then run your tests against the freshly changed configuration data.
如果另一张海报所说的是真的,并且您不希望创建一个全新的 XML 文件进行测试,那么我建议您将您的配置编辑放在测试方法本身中,然后针对新更改的配置运行您的测试数据。