zoukankan      html  css  js  c++  java
  • ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节集合

    核心代码

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    namespace Commons
    {  
        public class DefaultElement : ConfigurationElement
        {
            [ConfigurationProperty("factory")]
            public string Factory
            {
                get
                {
                    return this["factory"] as string;
                }
                set
                {
                    this["factory"] = value;
                }
            }
        } 
    
        public class FactoryElement : ConfigurationElement
        {
            [ConfigurationProperty("name")]
            public string Name
            {
                get
                {
                    return this["name"] as string;
                }
                set
                {
                    this["name"] = value;
                }
            }
    
            [ConfigurationProperty("assembly")]
            public string Assembly
            {
                get
                {
                    return this["assembly"] as string;
                }
                set
                {
                    this["assembly"] = value;
                }
            }
    
            [ConfigurationProperty("class")]
            public string Class
            {
                get
                {
                    return this["class"] as string;
                }
                set
                {
                    this["class"] = value;
                }
            }
        }
    
        public class FactoryElements : ConfigurationElementCollection
        {
            protected override ConfigurationElement CreateNewElement()
            {
                return new FactoryElement();
            }
    
            protected override object GetElementKey(ConfigurationElement element)
            {
                return ((FactoryElement)element).Name;
            }
    
            public FactoryElement this[string name]
            {
                get
                {
                    return BaseGet(name) as FactoryElement;
                }
            }
        }
    
        public class DbFactorySection : ConfigurationSection
        {
            [ConfigurationProperty("default")]
            public DefaultElement DefaultFactory
            {
                get
                {
                    return this["default"] as DefaultElement;
                }
                set
                {
                    this["default"] = value;
                }
            }
            [ConfigurationProperty("factorys")]
            public FactoryElements Factorys
            {
                get
                {
                    return this["factorys"] as FactoryElements;
                }
                set
                {
                    this["factorys"] = value;
                }
            }
        }
    
    }
    View Code

    配置文件

      <configSections>
        <section name="dbFactory" type="Commons.DbFactorySection"/>
      </configSections>
    
      <dbFactory >
        <default factory="sql"></default>
        <factorys>
          <add name="sql"  assembly="Hello.Data"  class="SqlDbFactory" />
          <add name="oracle" assembly="Hello.Data" class="OracleDbFactory" />
          <add name="sqlite" assembly="Hello.sqlite" class="SqliteDbFactory" />
          <!--还可以添加无数个<add 节点-->
        </factorys>
      </dbFactory>

    代码中使用

    DbFactorySection dfs = ConfigurationManager.GetSection("dbFactory") as DbFactorySection;
    FactoryElements fes = dfs.Factorys;
    FactoryElement feSql = fes["sql"];
    FactoryElement feSqlite = fes["sqlite"]
  • 相关阅读:
    创建索引
    列出所有的索引
    查看集群节点api
    集群健康检查api
    mapping 映射
    Elasticsearch 版本控制
    四种常见的 POST 提交数据方式
    HttpPost 传输Json数据并解析
    基本概念
    信用卡年轻消费群体数据分析和洞察报告
  • 原文地址:https://www.cnblogs.com/yonsy/p/5620580.html
Copyright © 2011-2022 走看看