我用C#,框架3.5(VS 2008)的工作。如何獲得類型的配置節的值NameValueSectionHandler
我使用的是ConfigurationManager
加載配置(不是默認app.config文件)爲配置對象。
使用配置類,我能得到一個ConfigurationSection
,但我不能找到一個辦法讓該段的值。
在配置中,ConfigurationSection
是System.Configuration.NameValueSectionHandler
類型。
對於它的價值,當我使用ConfigurationManager
的方法GetSection
(僅適用於我的默認app.config文件時),我收到了一個對象類型,價值,而我剛收到像Dictionary這樣的價值。但是,當我從配置類中收到ConfigurationSection
類時,我無法做這樣的演員。
編輯:配置文件的實施例 :
的方式我能夠使用它時,它是上的app.config<configuration>
<configSections>
<section name="MyParams"
type="System.Configuration.NameValueSectionHandler" />
</configSections>
<MyParams>
<add key="FirstParam" value="One"/>
<add key="SecondParam" value="Two"/>
</MyParams>
</configuration>
實施例(以下簡稱「GetSection」方法是默認的app.config ):
NameValueCollection myParamsCollection =
(NameValueCollection)ConfigurationManager.GetSection("MyParams");
Console.WriteLine(myParamsCollection["FirstParam"]);
Console.WriteLine(myParamsCollection["SecondParam"]);
你有沒有得到很好的解決了這一點? – 2014-06-24 04:11:52
如果你將使用.Net版本4.0,那麼動態可以幫助 – Rahul 2014-09-03 07:06:25