环境:
我的解决方案中目前有三个项目:
我的测试项目中的依赖项全部来自NuGet:
问题:
.NET标准库依赖于任何使用它的应用程序中存在的app.config文件。它使用ConfigurationSection和ConfigurationElement属性将值映射到类,与以下答案非常相似:具有嵌套集合的自定义配置节
.NET Core控制台应用程序中包含一个app.config文件,该库能够很好地解析出其中的值并使用它们。好极了。
另一方面,NUnit控制台应用程序具有相同的app.config文件,但是该库似乎看不到它。一旦尝试使用ConfigurationManager.GetSection("...")它读取值,就会返回null。
有人在这样的环境中获得过app.config文件与NUnit3一起工作吗?
我尝试过的
它看起来像它支持配置文件,但我不知道,如果文档指的是一些特殊的NUnit的配置文件或app.config文件。
在到目前为止编写的一个测试中,我还尝试了一些尝试,尝试以某种方式设置配置文件,例如使用建议AppDomain.CurrentDomain.SetData()(没有用,可能是因为NUnit3不支持AppDomain):
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:\Path\To\My\Tests\my_test_project_name.dll.config");
Run Code Online (Sandbox Code Playgroud)
尽管NUnit存储库中有一些测试似乎表明可以在NUnit3中使用配置文件,但是该特定测试文件仅在.NET 4.5演示项目中引用,而不在.NET Core演示项目中引用。
我使用IConfigurationSectionHandler接口来获取有关我的自定义配置部分的信息.但它已被弃用,我想改用ConfigurationSection.
如何使用此视图创建自定义ConfigurationSection并使用ConfigurationSection而不是IConfigurationSectionHandler:
<CustomSectionBlaBla>
<Parent name="DB">
<FirstChild value="someValue"/>
<SecondChild value="someValue"/>
<Parent/>
...
<Parent name="UI">
<FirstChild value="someValue"/>
<SecondChild value="someValue"/>
<Parent/>
<CustomSectionBlaBla/>
Run Code Online (Sandbox Code Playgroud) 我找到了自定义配置处理程序的一个很好的例子,并尝试将它用于我自己的实现.
我已经像这样设置了App.config:
<configSections>
<section name="DocumentationSettings" type="ConfigHandler.DocumentationSettings,Settings"/>
</configSections>
<DocumentationSettings>
<DocumentationSections>
<DocumentationSection Id="AAA">
<SectionDescription Value="SectionDescriptionAAA"/>
</DocumentationSection>
<DocumentationSection Id="BBB">
<SectionDescription Value="SectionDescriptionBBB"/>
</DocumentationSection>
<DocumentationSection Id="CCC">
<SectionDescription Value="SectionDescriptionCCC"/>
</DocumentationSection>
</DocumentationSections>
</DocumentationSettings>
Run Code Online (Sandbox Code Playgroud)
我使用此代码访问我的自定义配置:
DocumentationSettings documentationSettings = ConfigurationManager.GetSection("DocumentationSettings") as DocumentationSettings;
foreach (DocumentationSectionConfigElement section in (documentationSettings.DocumentationSections.Sections))
{
Console.WriteLine(section.Id);
Console.WriteLine(section.SectionDescription.Properties.Value);
}
Run Code Online (Sandbox Code Playgroud)
首先'Console.WriteLine'完美无缺.
所以我有以下问题和实施相关的问题:
第二个'Console.WriteLine'出错,错误:无法识别的属性'Value'.我已经放了"public SectionDescription SectionDescription",因为"DocumentationSectionConfigElement"类暴露了属性访问,但我可能错了,我先尝试将它放入"DocumentationSectionCollection",但我不知道如何在那里实现它,对我来说似乎是"DocumentationSectionCollection"仅实现"集合"逻辑.
我想要像这样访问"字段":
section.Id section.SectionDescription.Value
或者像这样:
section.Properties.Id
section.SectionDescription.Properties.Value
Run Code Online (Sandbox Code Playgroud)
我看到"Collection"允许使用这样的索引器方法直接使用这些属性:
public DocumentationSectionConfigElement this[int index]
Run Code Online (Sandbox Code Playgroud)
但我不能在"SectionDescription"类上实现索引器方法,因为它是一个单独的部分而不是集合,所以当我访问字段时,这个"属性"名称仍然存在.
为了能够在这些配置对象上使用LINQ,我需要添加什么?我的意思是这样的:
(documentationSettings.DocumentationSections.Sections).Select(x => x.Id)
复杂的XML结构配置处理程序是否有很好的例子?从我发现的那些大多数简单的结构,如:
但没有像这样的复杂结构的任何例子:
<section>
<subSections>
<subSection name="111">
<Description Value="AAA"></Description>
<Headers>
<Header type="Main">
<Properties>
<Property name="Property1"/> …Run Code Online (Sandbox Code Playgroud)