c#自己定义section配置节点及读取


MyDemoSection.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Da.Extend
{
    public class MyDemoSection : ConfigurationSection
    {
        [ConfigurationProperty("dbName", DefaultValue = "db.mdf")]
        public string DBName
        {
            get { return (string)this["dbName"]; }
            set { this["dbName"] = value; }
        }

        [ConfigurationProperty("loginName", DefaultValue = "sa")]
        public string LoginName
        {
            get { return (string)this["loginName"]; }
            set { this["loginName"] = value; }
        }
    }
}

 
SimpleSection.cs
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace Da.Extend
{
    /// <summary>
    /// 自定义ConfigurationSection
    /// </summary>
    public class SimpleSection : ConfigurationSection
    {
        /// <summary>
        /// 父节点集合属性定义
        /// </summary>
        [ConfigurationProperty("components")]
        public ComponentElements Components
        {
            get
            {
                return this["components"] as ComponentElements;
            }

            set
            {
                this["components"] = value;
            }
        }
    }

    /// <summary>
    /// 节点集合
    /// </summary>

    public class ComponentElements : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new ComponentElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ComponentElement)element).Id;
        }

        public ComponentElement this[string Id]
        {
            get
            {
                return BaseGet(Id) as ComponentElement;
            }
        }
    }

    /// <summary>
    /// 节点
    /// </summary>
    public class ComponentElement : ConfigurationElement
    {
        [ConfigurationProperty("id")]
        public string Id
        {
            get { return (string)this["id"]; }
            set { this["id"] = value; }
        }

        [ConfigurationProperty("service")]
        public string Service
         {
             get { return (string)this["service"]; }
             set { this["service"] = ""; }
         }

         [ConfigurationProperty("type")]
         public string Type
         {
             get { return (string)this["type"]; }
             set { this["type"] = ""; }
         }
    }
}
App.config
<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,Castle.Windsor"/>
    <section name="db" type="Da.Extend.MyDemoSection, Da.Extend"/>
    <section name="simple" type="Da.Extend.SimpleSection, Da.Extend"/>
  </configSections>
  <castle>
    <components>
      <component   id="IUserDao"   service="Da.Dao.IUserDao,Da.Dao"   type="Da.Dao.UserDao,Da.Dao" />
    </components>
  </castle>
  <db dbName ="app.mdf" loginName="admin" />
  <simple>
    <components>
      <add   id="IUserDao"   service="Da.Dao.IUserDao,Da.Dao"   type="Da.Dao.UserDao,Da.Dao" />
    </components>
  </simple>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>
调用示例代码:
 static void test4()
        {
           Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            MyDemoSection sect = config.Sections["db"] as MyDemoSection;
            if (sect != null)
            {
                string s =string.Format("数据库:{0},登录名:{0}。",sect.DBName,sect.LoginName);
                Console.WriteLine(s);
            }
        }

        static void test5()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            SimpleSection sect = config.GetSection("simple") as SimpleSection;
            if (sect != null)
            {
                ComponentElements comCollection = sect.Components;
                foreach (ComponentElement com in comCollection)
                {
                    var str1 = com.Id;
                    var str2 = com.Service;
                    var str3 = com.Type;
                }
                string str = "";
                
            }
        }
原文地址:https://www.cnblogs.com/joyet-john/p/7344542.html