2009-06-15 7 views
5

Ich habe meine eigenen benutzerdefinierten Konfigurationsabschnitte, möchte aber ein neues Element erstellen, das einfache Schlüssel/Werte enthält. Jetzt habe ich eine funktionierende Version, aber es scheint eine Menge Code für solch eine einfachere Aufgabe zu sein. Gibt es eine verbesserte Art, Dinge zu tun?Wie definiert man einen grundlegenden benutzerdefinierten Konfigurationsabschnitt?

Unten ist eine abgespeckte Version meiner Konfigurations- und Konfigurationsklasse.

web.config

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

Benutzerdefinierte Konfiguration Klasse

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

Versuchen Sie diesen Artikel: [Schreiben von benutzerdefinierten Konfigurationseinstellungen Abschnitt in C#] (http://www.codearsenal.net/2012/10/writing-custom-configuration-section-in-csharp.html) –

Antwort

9

Etwas Ähnliches Ich denke:

<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"); 

Update: in .NET 4.0 Ich verwende

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

Dies manuell zu tun erfordert viel zu viel Aufwand. Sie können Visual Studio den Abschnitt für Sie mit dem Configuration Section Designer-Add-In erstellen lassen.

+0

Erfordert Visual Stuiod 2008 oder größer. – David

+3

Tatsächlich wird VS 2010 noch nicht unterstützt. : o ( – Boydski

+0

Alpha-Code und Chrome hält es für schädlich. – stuartdotnet

Verwandte Themen