【发布时间】:2021-10-03 02:09:56
【问题描述】:
我正在使用一个程序,它需要处理一些所谓的“接口”。程序不知道这些“接口”的名称,但程序需要能够读取配置文件中提供的接口名称、字段的名称和值。
为了做到这一点,我创建了一个“App.config”文件,如下所示:
<configuration>
<configSections>
<sectionGroup name="interfaces">
<section name="IF1" type="System.Configuration.AppSettingsSection"/>
<section name="IF2" type="System.Configuration.AppSettingsSection"/>
<section name="IF3" type="System.Configuration.AppSettingsSection"/>
</sectionGroup>
</configSections>
<interfaces>
<IF1>
<add key="IF1.ENABLE" value="false"/>
</IF1>
<IF2>
<add key="IF2.ENABLE" value="true"/>
<add key="IF2.ENABLE_EVENTS" value="true"/>
<add key="IF2.MAX" value="1000"/>
<add key="IF2.MIN" value="100"/>
</IF2>
...
但是,为了读取配置文件中的信息,我使用了以下代码(有相应的问题):
NameValueCollection settings = ConfigurationManager.GetSection("interfaces") as NameValueCollection;
// returns "null"
NameValueCollection settings = ConfigurationManager.GetSection("interfaces/*") as NameValueCollection;
// returns "null"
NameValueCollection settings = ConfigurationManager.GetSection("interfaces/IF1") as NameValueCollection;
// works OK, but it means that the name of the interface needs to be known by the application.
在观察窗口中检查settings(从最后一行开始)时,我发现这是使用Add watch 查看第一个条目的值的方式(请不要笑):
new System.Collections.ArrayList.ArrayListDebugView(
((System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry)
(new System.Collections.Hashtable.HashtableDebugView(settings._entriesTable).Items[0]).Value).Value).Items[0]
尝试查看这不起作用,原因很简单,settings._entriesTable 是私有的,因此无法访问。
有没有人知道一个简单的说法:(伪代码)
foreach (entry) in configuration.interfaces
{
foreach (interface_entry) in configuration.interfaces.getSection(entry.name)
{
string keyname = interface_entry.key;
string valuename = interface_entry.value;
}
}
还是我的配置文件格式完全错误?
【问题讨论】:
标签: c# app-config configurationmanager