我有一个app.config部分我想定义,作为一个简单的IDictionary<string, IDictionary<string, string>>
会是这个样子的app.config文件:自定义配置节 - KISS方法
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="ServiceInstances" type="MyProject.Configuration, MyProject"/>
</configSections>
<ServiceInstances>
<ServiceInstance name="service1">
<kvp key="key1" value="value1"/>
<kvp key="key2" value="value2"/>
</ServiceInstance>
<ServiceInstance name="service2">
<kvp key="key3" value="value3"/>
<kvp key="key2" value="value2"/>
</ServiceInstance>
</ServiceInstances>
</configuration>
所有的教程我“M看到似乎得到相当深成这样,我只是在寻找一个快速和肮脏的方式做到这一点:
IDictionary<string, string> foo = Configuration.GetDictionary("service1");
IDictionary<string, string> bar = Configuration.GetDictionary("service2");
看起来这应该从字面上的几行代码,但似乎教程不必要地使其复杂化。有没有一个快速的答案,如果是这样,有人可以告诉我它应该是什么样子?
这强烈地键入配置设置;将其转换为字典对象似乎很平凡。 – 2012-08-06 15:59:54
原来它并不平庸:向部分类方法'ToDictionary'添加到生成的'ServiceInstance'类中,返回'this.Properties.OfType()。ToDictionary(key => key.Name,value => this [value .Name] .ToString())'这似乎做我想要的。 –
2012-08-06 16:12:03