2011-11-02 26 views
12

Tôi sử dụng giao diện IConfigurationSectionHandler để nhận thông tin về phần cấu hình tùy chỉnh của tôi. Nhưng nó không được dùng nữa và tôi muốn sử dụng ConfigurationSection.tùy chỉnh ConfigurationSection

Làm thế nào để tạo ConfigurationSection tùy chỉnh với quan điểm này và sử dụng ConfigurationSection thay IConfigurationSectionHandler:

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

Trả lời

10

Bạn nên kiểm tra hàng loạt ba phần Jon RISTA trên NET cấu hình 2.0 lên trên CodeProject.

Rất khuyến khích, cũng bằng văn bản và cực kỳ hữu ích!

Nó sẽ cho bạn thấy làm thế nào để có được kết quả mong muốn của bạn - từng bước.

Mục khác để kiểm tra là Configuration Section Designer - một bổ trợ Visual Studio cho phép bạn thiết kế trực quan các phần cấu hình của bạn và CSD tạo tất cả các lớp cần thiết cho bạn - rất khuyến khích!

+0

+1 này, điều này và chỉ này. Mặc dù, đối với một cái nhìn tổng quan ít hơn một chút về loại cấu hình cơ bản nhất [Phần cấu hình tùy chỉnh trong 3 bước dễ dàng] (http://haacked.com/archive/2007/03/11/custom-configuration-sections-in- 3-easy-steps.aspx). –

19

Dưới đây là ví dụ về phần cấu hình mà tôi đã tạo. Nên chỉ cho bạn đi đúng hướng.

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

Và ở đây nó đang được sử dụng trong 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

Cảm ơn bạn. Tôi nghĩ rằng đây là những gì tôi cần. –

+2

Để hoàn thành, hãy thực hiện với: 'ImportConfiguration columns = (ImportConfiguration) ConfigurationManager.GetSection (" importConfiguration "); foreach (ImportColumnMapElement col trong cột.ImportMap) { \t Debug.Write (col.localName + col.sourceName); } ' – amackay11

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