2009-06-15 23 views
5

Mam własne sekcje konfiguracji niestandardowej, ale chciałbym utworzyć nowy element, który zawiera w sobie proste klucze/wartości. Teraz mam działającą wersję, ale wydaje się całkiem sporo kodu na tak prostsze zadanie. Czy istnieje ulepszony sposób robienia rzeczy?Jak zdefiniować podstawową sekcję konfiguracji niestandardowej?

Poniżej znajduje się obnażona wersja mojej konfiguracji i niestandardowej klasy konfiguracji.

Klasa web.config

<myRootNode> 
    <myNode> 
     <add key="a" value="" /> 
     <add key="b" value="" /> 
     <add key="c" value="" /> 
     ... 
    </myNode> 
    ...any other nodes 
</myRootNode> 

Konfiguracja użytkownika

public class MyRootNode : ConfigurationSection 
{ 
    [ConfigurationProperty("myNode")] 
    public MyNodeElement MyNode 
    { 
     get { return (MyNodeElement)this["myNode"]; } 
    } 
} 

[ConfigurationCollection(typeof(BaseElement), AddItemName = "add")] 
public class MyNodeElement : ConfigurationElementCollection 
{ 
    protected override ConfigurationElement CreateNewElement() 
    { 
     return new BaseElement(); 
    } 

    protected override object GetElementKey(ConfigurationElement element) 
    { 
     return ((BaseElement)element).Key; 
    } 

    public BaseElement this[int index] 
    { 
     get { return this.BaseGet(index) as BaseElement; } 
    } 
} 

public class BaseElement : ConfigurationElement 
{ 
    [ConfigurationProperty("key", IsRequired = true, IsKey = true)] 
    public string Key 
    { 
     get { return this["key"].ToString(); } 
    } 

    [ConfigurationProperty("value", IsRequired = true)] 
    public string Value 
    { 
     get { return this["value"].ToString(); } 
    } 
} 
+0

Spróbuj tego artykułu: [Pisanie sekcji ustawień konfiguracji niestandardowej w języku C#] (http://www.codearsenal.net/2012/10/writing-custom-configuration-section-in-csharp.html) –

Odpowiedz

9

Coś takiego Chyba:

<configSections> 
     <section name="validationXsds" type="System.Configuration.DictionarySectionHandler, System" /> 
    </configSections> 


    <validationXsds> 
    <add key="http://schemas.xmlsoap.org/soap/envelope/" value="http://dev.ei.directv.com/schemas/xmlsoap/envelope.xsd"/> 
    <add key="http://schemas.xmlsoap.org/soap/encoding/" value="http://dev.ei.directv.com/schemas/xmlsoap/encoding.xsd"/> 
    <add key="http://ei.directv.com/schemas/envelope/v3_0" value="http://dev.ei.directv.com/schemas/envelope/v3.0/Envelope.xsd"/> 
    </validationXsds> 

IDictionary xsds = (IDictionary)WebConfigurationManager.GetSection("validationXsds"); 

Aktualizacja: w .NET 4.0 Używam

type="System.Configuration.NameValueSectionHandler, System, Version=4.0.0.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" 
6

Robienie tego ręcznie wymaga zbyt wiele wysiłku. Możesz mieć Visual Studio utworzyć sekcję dla ciebie z dodatkiem Configuration Section Designer.

+0

Wymaga Visual Stuiod 2008 albo lepszy. – David

+3

W rzeczywistości VS 2010 nie jest jeszcze obsługiwany. : o ( – Boydski

+0

Kod alfa i Chrome są złośliwe. – stuartdotnet

Powiązane problemy