2011-11-02 143 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