我找到了自定义配置处理程序的一个很好的例子,并尝试将它用于我自己的实现.
我已经像这样设置了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)