2011-11-02 22 views
12

Używam interfejsu IConfigurationSectionHandler, aby uzyskać informacje o mojej sekcji niestandardowej konfiguracji. Ale jest przestarzałe i zamiast tego chcę użyć ConfigurationSection.Ustawienia niestandardowe Ustawienia

Jak utworzyć niestandardowy ConfigurationSection się z tym poglądem i korzystając ConfigurationSection zamiast IConfigurationSectionHandler:

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

Odpowiedz

10

należy sprawdzić trzyczęściową serię Jon Rista jest na konfigurację .NET 2.0 aż na CodeProject.

Gorąco polecam, dobrze napisana i bardzo pomocny!

Powinno pokazać, jak uzyskać pożądany wynik - krok po kroku.

Innym elementem do sprawdzenia jest Configuration Section Designer - dodatek Visual Studio, który pozwoli Ci wizualnie zaprojektować sekcje konfiguracji, a CSD utworzy dla ciebie wszystkie potrzebne klasy - gorąco polecane!

+0

+1 to, to i tylko to. Chociaż dla nieco mniej zaangażowanego przeglądu najbardziej podstawowego typu sekcji konfiguracji [sekcja Custom Config w 3 prostych krokach] (http://haacked.com/archive/2007/03/11/custom-configuration-sections-in- 3-easy-steps.aspx). –

19

Oto przykład sekcji konfiguracji, którą utworzyłem. Powinien wskazać ci właściwy kierunek.

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; 
    } 
} 

I tutaj jest on używany w pliku 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

Dziękuję. Myślę, że tego właśnie potrzebuję. –

+2

Dla kompletności, implementuj z: 'ImportConfiguration columns = (ImportConfiguration) ConfigurationManager.GetSection (" importConfiguration "); foreach (ImportColumnMapElement col in columns.ImportMap) { \t Debug.Write (col.localName + col.sourceName); } ' – amackay11

Powiązane problemy