2011-02-17 69 views
19

我沒有找到如何在app.config中嵌套配置節的app.config

<my.configuration> 
    <emailNotification> 
     <to value="[email protected]" /> 
     <from value="[email protected]" /> 
     <subject value="Subject" /> 
     <smtpHost value="smtp.you.com" /> 
     <triggers> 
     <add name="1" varAlias="Var1" lower="-200" upper="-150"/> 
     </triggers> 
    </emailNotification> 
    </my.configuration> 

我以前用過ConfigurationElementCollection中和的ConfigurationElement訪問這樣一個嵌套的結構部分的任何實例。但我不知道如何做到以上?

+0

我假定`my.configuration`是一組和`emailNotification`是嵌套部分你指的是。正確? – 2011-02-17 10:34:41

+0

my.configuration是一個節

和emailNotification組,是 – chriszero 2011-02-17 11:22:08

回答

35

您需要:

定義my.configuration爲欄目組和emailNotification爲組內的部分。添加下面的配置文件:

<configSections> 
    <sectionGroup name="my.configuration" 
        type="SectionGroupRetrieval.MyConfigurationGroup, SectionGroupRetrieval"> 
     <section name="emailNotification" 
       type="SectionGroupRetrieval.EmailNotificationSection, SectionGroupRetrieval" /> 
    </sectionGroup>  
</configSections> 

實現配置節組(my.configuration)。

public class MyConfigurationGroup : ConfigurationSectionGroup 
{ 
    [ConfigurationProperty("emailNotification")] 
    public EmailNotificationSection EmailNotification 
    { 
     get { return (EmailNotificationSection)base.Sections[ "emailNotification" ]; } 
    } 
} 

執行配置部分(emailNotification)。

public class EmailNotificationSection : ConfigurationSection 
{ 
    [ConfigurationProperty("to")] 
    public ValueElement To 
    { 
     get { return (ValueElement)base[ "to" ]; } 
    } 

    [ConfigurationProperty("from")] 
    public ValueElement From 
    { 
     get { return (ValueElement)base[ "from" ]; } 
    } 

    [ConfigurationProperty("subject")] 
    public ValueElement Subject 
    { 
     get { return (ValueElement)base[ "subject" ]; } 
    } 

    [ConfigurationProperty("smtpHost")] 
    public ValueElement SmtpHost 
    { 
     get { return (ValueElement)base[ "smtpHost" ]; } 
    } 

    [ConfigurationProperty("triggers")] 
    public TriggerElementCollection Triggers 
    { 
     get { return (TriggerElementCollection)base[ "triggers" ]; } 
    } 
} 

實現必要的配置元素和配置元素集合。

public class ValueElement : ConfigurationElement 
{ 
    [ConfigurationProperty("value")] 
    public string Value 
    { 
     get { return (string)base[ "value" ]; } 
     set { base[ "value" ] = value; } 
    } 
} 

public class TriggerElement : ConfigurationElement 
{ 
    [ConfigurationProperty("name")] 
    public string Name 
    { 
     get { return (string)base[ "name" ]; } 
     set { base[ "name" ] = value; } 
    } 

    [ConfigurationProperty("varAlias")] 
    public string VarAlias 
    { 
     get { return (string)base[ "varAlias" ]; } 
     set { base[ "varAlias" ] = value; } 
    } 

    [ConfigurationProperty("lower")] 
    public int Lower 
    { 
     get { return (int)base[ "lower" ]; } 
     set { base[ "lower" ] = value; } 
    } 

    [ConfigurationProperty("upper")] 
    public int Upper 
    { 
     get { return (int)base[ "upper" ]; } 
     set { base[ "upper" ] = value; } 
    } 
} 

[ConfigurationCollection(typeof(TriggerElement))] 
public class TriggerElementCollection : ConfigurationElementCollection 
{ 
    public TriggerElement this[ string name ] 
    { 
     get { return (TriggerElement)base.BaseGet(name); } 
    } 

    public TriggerElement this[ int index ] 
    { 
     get { return (TriggerElement)base.BaseGet(index); } 
    } 

    protected override ConfigurationElement CreateNewElement() 
    { 
     return new TriggerElement(); 
    } 

    protected override object GetElementKey(ConfigurationElement element) 
    { 
     return ((TriggerElement)element).Name; 
    } 
} 

更新配置文件和執行必要的配置位後,便可輸入部分,如下所示:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
MyConfigurationGroup myConfiguration = (MyConfigurationGroup)config.GetSectionGroup("my.configuration"); 
EmailNotificationSection section = myConfiguration.EmailNotification;