嵌套配置部分 app.config [英] Nested Configuration Section app.config

查看:32
本文介绍了嵌套配置部分 app.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I don't find any examples of how to access such a nested configuration section in a 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.但是我不知道怎么做上面的?

I used ConfigurationElementCollection and ConfigurationElement before. But I don't know how to do the above?

推荐答案

您需要:

my.configuration 定义为部分组,将 emailNotification 定义为组内的一个部分.在配置文件中添加以下内容:

Define my.configuration as section group and emailNotification as a section within the group. Add following to the configuration file:

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

实现必要的配置元素和配置元素集合.

Implement necessary configuration elements and configuration element collection.

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

更新配置文件并实现必要的配置位后,您可以按如下方式访问您的部分:

After updating the configuration file and implementing necessary configuration bits, you can access you section as follows:

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

这篇关于嵌套配置部分 app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆