• 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 = "";
                    
                }
            }
  • 相关阅读:
    ext DateTime.js在ie下显示不全
    js 获得每周周日到周一日期
    近十年one-to-one最短路算法研究整理【转】
    虚函数(实现多态)
    函数调用机制2
    函数调用机制
    面向对象的三大特性
    矩阵类c++实现
    矩阵求逆c++实现
    解决文件大小上传限制
  • 原文地址:https://www.cnblogs.com/joyet-john/p/7344542.html
Copyright © 2020-2023  润新知