2011-11-02 139 views
12

我使用IConfigurationSectionHandler接口獲取有關我的自定義配置部分的信息。但它已過時,我想用ConfigurationSection代替。自定義配置部分

如何創建這種觀點的自定義配置節和使用的ConfigurationSection代替IConfigurationSectionHandler:

<CustomSectionBlaBla> 
    <Parent name="DB"> 
     <FirstChild value="someValue"/> 
     <SecondChild value="someValue"/> 
    <Parent/> 
    ... 
    <Parent name="UI"> 
     <FirstChild value="someValue"/> 
     <SecondChild value="someValue"/> 
    <Parent/> 
<CustomSectionBlaBla/> 

回答

10

您應該.NET退房喬恩Rista的三部系列2.0配置了CodeProject上。

強烈推薦,寫得很好,非常有幫助!

它應該告訴你如何達到你想要的結果 - 一步一步來。

其他要檢查的項目是Configuration Section Designer - 一個Visual Studio插件,它可以讓你在視覺上設計你的配置部分,並讓CSD爲你創建所有必要的類 - 強烈推薦!

+0

+1這,這也只有這個。儘管對於最基本類型的配置部分[3個簡單步驟中的自定義配置部分]略有涉及的概述(http://haacked.com/archive/2007/03/11/custom-configuration-sections-in- 3-易於steps.aspx)。 –

19

下面是我創建的配置部分的示例。應該指出你在正確的方向。

public class ImportConfiguration : ConfigurationSection 
{ 
    [ConfigurationProperty("importMap")] 
    public ImportMapElementCollection ImportMap 
    { 
     get 
     { 
      return this["importMap"] as ImportMapElementCollection; 
     } 
    } 
} 

public class ImportColumnMapElement : ConfigurationElement 
{ 
    [ConfigurationProperty("localName", IsRequired = true, IsKey = true)] 
    public string LocalName 
    { 
     get 
     { 
      return this["localName"] as string; 
     } 
     set 
     { 
      this["localName"] = value; 
     } 
    } 

    [ConfigurationProperty("sourceName", IsRequired = true)] 
    public string SourceName 
    { 
     get 
     { 
      return this["sourceName"] as string; 
     } 
     set 
     { 
      this["sourceName"] = value; 
     } 
    } 
} 

public class ImportMapElementCollection : ConfigurationElementCollection 
{ 
    public ImportColumnMapElement this[object key] 
    { 
     get 
     { 
      return base.BaseGet(key) as ImportColumnMapElement; 
     } 
    } 

    public override ConfigurationElementCollectionType CollectionType 
    { 
     get 
     { 
      return ConfigurationElementCollectionType.BasicMap; 
     } 
    } 

    protected override string ElementName 
    { 
     get 
     { 
      return "columnMap"; 
     } 
    } 

    protected override bool IsElementName(string elementName) 
    { 
     bool isName = false; 
     if (!String.IsNullOrEmpty(elementName)) 
      isName = elementName.Equals("columnMap"); 
     return isName; 
    } 

    protected override ConfigurationElement CreateNewElement() 
    { 
     return new ImportColumnMapElement(); 
    } 

    protected override object GetElementKey(ConfigurationElement element) 
    { 
     return ((ImportColumnMapElement)element).LocalName; 
    } 
} 

這裏,它在web.config中使用:

<importConfiguration> 
    <importMap> 
     <columnMap localName="PropertyID" sourceName="Detail Number"/> 
     <columnMap localName="DateOfOpen" sourceName="Open Date &amp; Time"/> 
     <columnMap localName="StartTime" sourceName="Open Date &amp; Time"/> 
     <columnMap localName="ClosingTime" sourceName="Close Date &amp; Time"/> 
     <columnMap localName="StreetAddress" sourceName="Street Address"/> 
    </importMap> 
</importConfiguration> 
+0

謝謝。我認爲這是我需要的。 –

+2

爲了完整起見,使用: 'ImportConfiguration columns =(ImportConfiguration)ConfigurationManager.GetSection(「importConfiguration」); foreach(ImportColumnMapElement col in columns.ImportMap) { \t Debug.Write(col.localName + col.sourceName); }' – amackay11