.net架構
.NET架構通過基於XML的配置使配置設定駕輕就熟。它同時還提供了必要的方法,通過集合類(Collection classes)訪問這些設定。
通過一個靜態ConfigurationSettings類可訪問實際的配置資料。該類還提供了一個GetConfig()方法,可向一個合適的集合返回一個對象。本文中,我將示範三種可用來訪問和儲存配置資訊的方法。
應用配置資料存放區在App.config檔案,並由configSections節點定義。每一section都有一個type屬性定義。這裡我將討論的3個類型為NameValueSectionHandler、SingleTagSectionHandler和DictionarySectionHandler。你可以用一個sectionGroup元素定義節組。以下是一個配置節定義的例子:
<section name="MyCustomSection"
type="System.Configuration.NameValueSectionHandler"/>
建議使用type="System.Configuration.NameValueSectionHandler,System,Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"
節組是嵌入一個sectionGroup元素的獨立配置節。以下是一個節組的例子:
<sectionGroup name="CustomGroup">
<section name="Custom1"
type="System.Configuration.NameValueSectionHandler"/>
<section name="Custom2" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
最後,你所指定的配置節將用於構造儲存配置資料的自訂的XML節點。若要向配置節添加資料,只要將該配置節作為一個XML節點包含進去,並用add節點添加Collection資料。下例為一個NameValueSectionHandler配置節:
<MyCustomSection>
<add key="key1" value="value1"/>
<add key="key2" value="value2"/>
</MyCustomSection>
MyCustomSection程式段包含一個命名值集合,其兩個入口由key1和key2定義。
SingleTagSectionHandler較容易構造。正如NameValueSectionHandler,配置節可在configSections節點中找到。但在SingleTagSectionHandlers和NameValueSectionHandlers中,配置資料的添加方式是不同的,如下所示:
. . .
<section name="MySingleTagSection"
type="System.Configuration.SingleTagSectionHandler"/>
. . .
<MySingleTagSection setting1="value1" setting2="value2" setting3="value3"/>
. . .
DictionarySectionHandler與NameValueSectionHandler相似,但前者返回hashtable,後者返回NameValueCollection。當訪問大量配置值時,hashtable要快於NameValueCollectio。DictionarySectionHandler與NameValueSectionHandler的構造方式相同,如下例:
. . .
<section name="MyDictionarySection"
type="System.Configuration.DictionarySectionHandler"/>
. . .
<MyDictionarySection>
<add key="key1" value="value1"/>
</MyDictionarySection>
. . .
我自己用了一下,報錯。。原因還挺特別
System.Configuration.DictionarySectionHandler,System,Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089
沒有辦法把type改為以上那段。終於行了。
構造節組的方法與構造單獨配置節的方法基本相同,唯一的不同在於前者的自訂節點互相嵌套。借用前面的節組定義,以下是對節組的實現:
<CustomGroup>
<Custom1>
<add key="key1" value="value1"/>
</Custom1>
<Custom2>
<add key="key1" value="value1"/>
</Custom2>
</CustomGroup>
通過System.Configuration.ConfigurationSettings命名空間的GetConfig()方法和自訂配置節的串值來訪問應用配置設定,然後將該方法的結果轉為合適的類型。
對於SingleTagSectionHandler,將結果轉為System.Collections命名空間的IDictionary介面類型。對於NameValueSectionHandler,結果轉為在System.Collections.Specialized命名空間中定義的NameValueCollection類型。最後,對於DictionarySectionHandler,結果轉為System.Collections命名空間中的Hashtable類型。
對於節組,唯一的區別是,將加上正斜杠和配置節名的節組名作為字串參數傳遞給GetConfig()方法,以訪問自訂設定。
以下是一個使用這些自訂設定的執行個體:
System.Collections.IDictionary stsh = (System.Collections.IDictionary)
System.Configuration.ConfigurationSettings.GetConfig("MySingleTagSection");
System.Collections.Specialized.NameValueCollection nvsh =
(System.Collections.Specialized.NameValueCollection)
System.Configuration.ConfigurationSettings.GetConfig("MyNameValueSection");
System.Collections.Hashtable dsh = (System.Collections.Hashtable)
System.Configuration.ConfigurationSettings.GetConfig("MyDictionarySection");
System.Collections.Specialized.NameValueCollection sgnvsh =
(System.Collections.Specialized.NameValueCollection)
System.Configuration.ConfigurationSettings.GetConfig("MySectionGroup/MySection
1");
System.Diagnostics.Debug.WriteLine((string)stsh["sample1"]);
System.Diagnostics.Debug.WriteLine((string)nvsh["key1"]);
System.Diagnostics.Debug.WriteLine((string)dsh["key1"]);
System.Diagnostics.Debug.WriteLine((string)sgnvsh["key1"]);
以下是用於上面代碼的一段配置XML代碼:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="MySingleTagSection"
type="System.Configuration.SingleTagSectionHandler"/>
<section name="MyDictionarySection"
type="System.Configuration.DictionarySectionHandler"/>
<section name="MyNameValueSection"
type="System.Configuration.NameValueSectionHandler"/>
<sectionGroup name="MySectionGroup">
<section name="MySection1"
type="System.Configuration.NameValueSectionHandler"/>
<section name="MySection2"
type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<MySingleTagSection sample1="value1" sample2="value2" sample3="value3"/>
<MyDictionarySection>
<add key="key1" value="value1"/>
<add key="key2" value="value2"/>
</MyDictionarySection>
<MyNameValueSection>
<add key="key1" value="value1"/>
<add key="key2" value="value2"/>
</MyNameValueSection>
<MySectionGroup>
<MySection1>
<add key="key1" value="value1"/>
<add key="key2" value="value2"/>
</MySection1>
<MySection2>
<add key="key1" value="value1"/>
<add key="key2" value="value2"/>
</MySection2>
</MySectionGroup>
</configuration>
欲知更多資訊,請訪問MSDN Library中的application configuration settings和configuration schema。