zoukankan      html  css  js  c++  java
  • 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 = "";
                    
                }
            }
  • 相关阅读:
    递归神经网络(RNN)简介(转载)
    递归神经网络入门教程(转载)
    向量叉积的几何意义(转)
    向量点乘(内积)和叉乘(外积、向量积)概念及几何意义解读
    完全搞懂傅里叶变换和小波(6)――傅立叶级数展开之函数项级数的性质
    完全搞懂傅里叶变换和小波(5)——傅立叶级数展开之函数项级数的概念
    完全搞懂傅里叶变换和小波(4)——欧拉公式及其证明
    完全搞懂傅里叶变换和小波(3)——泰勒公式及其证明
    完全搞懂傅里叶变换和小波(2)——三个中值定理<转载>
    完全搞懂傅里叶变换和小波(1)——总纲<转载>
  • 原文地址:https://www.cnblogs.com/joyet-john/p/7344542.html
Copyright © 2011-2022 走看看