Nested Configuration Section app.config

前端 未结 1 1457
渐次进展
渐次进展 2020-12-10 01:27

I don\'t find any examples of how to access such a nested configuration section in a app.config

  
    
             


        
相关标签:
1条回答
  • 2020-12-10 01:48

    You need to:

    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>
    

    Implement the configuration section group (my.configuration).

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

    Implement the configuration section (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;
    
    0 讨论(0)
提交回复
热议问题