0

我希望我只是缺少一些简单的东西。我需要读/写我的 exe.config 文件的一部分。我的代码中有这个:

var appConfiguration = ConfigurationManager.OpenExeConfiguration("Mytest.Console.exe");
var fileEnvironment = appConfiguration.GetSection("fileEnvironment");

这是我的 app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="fileEnvironment" type="System.Configuration.DictionarySectionHandler"/>
    </configSections>

    <fileEnvironment>
        <add key="TestEntry1" value="A nice value"/>
        <add key="TestEntry2" value="Another value"/>
    </fileEnvironment>
</configuration>

我的 appConfiguration 变量返回为 {System.Configuration.Configuration} 并且“HasFile”属性设置为 true。

不强制转换我的变量“fileEnvironment”返回为System.Configuration.DefaultSection. 当我添加IDictionary<string, string>GetSection方法 fileEnvironment 时为空。

有任何想法吗?

4

2 回答 2

0

我一直在研究字典问题并想出了这个 stackoverflow Q&A!它生成一个集合而不是字典,但指出了解决方案的方法。感谢大家的时间。

于 2012-01-21T20:54:16.243 回答
0

根据这篇旧文章,当使用 实现部分时DictionarySectionHandler,将ConfigurationManager.GetSection()返回非泛型IDictionary,而不是IDictionary<T,V>. 这就是为什么你的演员失败了。

尽管在现代,看起来它实际上返回了一个HashTable.

于 2014-10-18T04:49:07.917 回答