繁体   English   中英

嵌套配置部分app.config

[英]Nested Configuration Section app.config

我没有找到如何在app.config中访问这样的嵌套配置部分的任何示例

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

我之前使用过ConfigurationElementCollection和ConfigurationElement。 但我不知道怎么做以上?

你需要:

my.configuration定义为section group,将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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM