2011-11-02 15 views
12

IConfigurationSectionHandlerインターフェイスを使用して、自分のカスタム設定セクションに関する情報を取得します。しかし、それは非難され、代わりにConfigurationSectionを使用したいと思います。あなたは.NET 2.0 CodeProjectの上の構成までにジョンRistaの3回シリーズをチェックアウトする必要がありますcustom ConfigurationSection

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

答えて

10

:どのようにこのビューでカスタムのConfigurationSectionを作成し、代わりにIConfigurationSectionHandlerをのConfigurationSectionを使用して

強くお勧めします、よく書かれており、非常に便利!

希望の結果に到達する方法を段階的に示す必要があります。

チェックアウトする他のアイテムはConfiguration Section Designerです。これはVisual Studioアドインで、設定セクションを視覚的にデザインし、必要なすべてのクラスを作成することをお勧めします。

+0

+1これ、これとこれだけ。しかし、最も基本的なタイプの設定セクション[Custom Configセクションの3つの簡単なステップ](http://haacked.com/archive/2007/03/11/custom-configuration-sections-in-http: 3-easy-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