2009-06-15 37 views
5

Tôi có các phần cấu hình tùy chỉnh của riêng mình nhưng muốn tạo một phần tử mới có khóa/giá trị đơn giản bên trong nó. Bây giờ tôi có một phiên bản làm việc nhưng có vẻ như khá nhiều mã cho một nhiệm vụ đơn giản như vậy. Có cách cải tiến để làm mọi thứ không?Cách xác định phần cấu hình tùy chỉnh cơ bản?

Dưới đây là phiên bản rút gọn của cấu hình và lớp cấu hình tùy chỉnh của tôi.

web.config

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

Tuỳ chỉnh cấu hình lớp

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

Hãy thử bài viết này: [Viết phần Cài đặt cấu hình tùy chỉnh trong C#] (http://www.codearsenal.net/2012/10/writing-custom-configuration-section-in-csharp.html) –

Trả lời

9

Something như thế này tôi đoán:

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

Cập nhật: trong .NET 4.0 Tôi đang sử dụng

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

Làm điều này bằng tay đòi hỏi quá nhiều nỗ lực. Bạn có thể có Visual Studio tạo phần cho bạn với bổ trợ Configuration Section Designer.

+0

Yêu cầu Visual Stuiod 2008 hoặc tuyệt hơn. – David

+3

Thực tế, VS 2010 chưa được hỗ trợ. : o ( – Boydski

+0

Mã Alpha và Chrome cho rằng nó độc hại. – stuartdotnet

Các vấn đề liên quan