2011-11-02 7 views
12

मैं अपने कस्टम कॉन्फ़िगरेशन अनुभाग के बारे में जानकारी प्राप्त करने के लिए IConfigurationSectionHandler इंटरफ़ेस का उपयोग करता हूं। लेकिन यह बहिष्कृत है और मैं इसके बजाय कॉन्फ़िगरेशनसेक्शन का उपयोग करना चाहता हूं।कस्टम कॉन्फ़िगरेशनसेक्शन

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

उत्तर

10

आप बाहर जॉन Rista के तीन भाग श्रृंखला नेट पर 2.0 विन्यास ऊपर CodeProject पर जांच होनी चाहिए:

कैसे और इस दृश्य के साथ कस्टम ConfigurationSection ConfigurationSection उपयोग करने के बजाय IConfigurationSectionHandler बनाने के लिए।

अत्यधिक की सिफारिश की है, अच्छी तरह से लिखा और अत्यंत उपयोगी!

यह आपको दिखाएगा कि वांछित परिणाम कैसे प्राप्त करें - चरण-दर-चरण।

अन्य आइटम चेक आउट करने के लिए Configuration Section Designer - एक विजुअल स्टूडियो ऐड-इन जो आपको अपने कॉन्फ़िगरेशन अनुभागों को दृष्टि से डिज़ाइन करने की अनुमति देगा, और सीएसडी आपके लिए सभी आवश्यक कक्षाएं तैयार करेगा - अत्यधिक अनुशंसित!

+0

+1 यह, इस और केवल इस। हालांकि, सबसे बुनियादी प्रकार के कॉन्फ़िगरेशन अनुभाग [कस्टम कॉन्फ़िगरेशन अनुभाग 3 आसान चरणों में] के थोड़ा कम शामिल अवलोकन के लिए (http://haacked.com/archive/2007/03/11/custom-configuration-sections-in- 3-आसान steps.aspx)। –

19

यहां मैंने बनाए गए कॉन्फ़िगरेशन सेक्शन का एक उदाहरण दिया है। आपको सही दिशा में इंगित करना चाहिए।

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

और यहाँ यह 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

धन्यवाद। मुझे लगता है कि मुझे यही चाहिए। –

+2

पूर्णता के लिए, 'आयात कॉन्फ़िगरेशन कॉलम = (आयात कॉन्फ़िगरेशन) कॉन्फ़िगरेशन प्रबंधक। गेटसेक्शन ("आयात कॉन्फ़िगरेशन"); foreach (कॉलम में आयातकॉलमैपमैलेमेंट कोल। आयातमैप) { \t डीबग.राइट (col.localName + col.sourceName); } ' – amackay11

संबंधित मुद्दे