我有一個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