zoukankan      html  css  js  c++  java
  • 自定义配置app.config

    先要写配置类,然后封装成DLL
    代码
    namespace CustomConfig
    {
        
    public   class DbFactorySection:System.Configuration.ConfigurationSection
        {
            [System.Configuration.ConfigurationProperty(
    "default")]
            
    public DefaultElement DefaultFactory
            {
                
    get
                {
                    
    return this["default"as DefaultElement;
                }
                
    set
                {
                    
    this["default"= value;
                }     
            }
            [System.Configuration.ConfigurationProperty(
    "factorys")]
            
    public FactoryElements Factorys
            {
                
    get
                {
                    
    return this["factorys"as FactoryElements;
                }
                
    set
                {
                    
    this["factorys"= value;
                }
            
            }

        }
    }
    代码
    namespace CustomConfig
    {
        
    public  class DefaultElement: System.Configuration.ConfigurationElement
        {
            [System.Configuration.ConfigurationProperty(
    "factory")]
            
    public string Factory
            {
                
    get
                {
                    
    return this["factory"as string;
                }
                
    set
                {
                    
    this["factory"= value;
                }
            
            }
        }
    }
    代码
    namespace CustomConfig
    {
        
    public class FactoryElement:System.Configuration.ConfigurationElement
        {
            [System.Configuration.ConfigurationProperty(
    "name")]
            
    public string Name
            {
                
    get
                {
                    
    return this["name"as string;
                }
                
    set
                {
                    
    this["name"= value;
                }
            }
            [System.Configuration.ConfigurationProperty(
    "assembly")]
            
    public string Assembly
            {
                
    get
                {
                    
    return this["assembly"as string;
                }
                
    set
                {
                    
    this["assembly"= value;
                }
            }
            [System.Configuration.ConfigurationProperty(
    "class")]
            
    public string Class
            {
                
    get
                {
                    
    return this["class"as string;
                }
                
    set
                {
                    
    this["class"= value;
                }
            }
               
        }
    }
    代码
    namespace CustomConfig
    {
        
    public  class FactoryElements: System.Configuration.ConfigurationElementCollection
        {
            
    protected override System.Configuration.ConfigurationElement CreateNewElement()
            {
                
    return new FactoryElement();
            }

            
    protected override object GetElementKey(System.Configuration.ConfigurationElement element)
            {
                
    return ((FactoryElement)element).Name;   
            }

            
    /// <summary>
            
    /// 用于在元素集合中指定名字
            
    /// </summary>
            
    /// <param name="name"></param>
            
    /// <returns></returns>
            public FactoryElement this[string name]
            {
                
    get
                {
                    
    return BaseGet(name) as FactoryElement;
                }
            }
        }
    }

    在app.config中加入以下节点:

    代码
    <configuration>
        
    <configSections>
          
    <section  name="dbFactory" type="CustomConfig.DbFactorySection,CustomConfig"/>
        
    </configSections>
      
    <dbFactory>
        
    <default factory="sql">
        
    </default>
        
    <factorys>
          
    <add name="sql" assembly="Hello.Data" class="SqlDbFactory"/>
          
    <add name="oracle" assembly="Hello.Data" class="OracleDbFactory"/>
        
    </factorys>
      
    </dbFactory>

    后台中调用:

     DbFactorySection conSection = System.Configuration.ConfigurationManager.GetSection("dbFactory") as CustomConfig.DbFactorySection;

                string strName = conSection.Factorys["sql"].Name.ToString();

    可以得到配置节点的信息.

    转自:http://www.cnblogs.com/zhaoguihua/archive/2010/01/21/1652847.html

  • 相关阅读:
    堆和堆排序
    快速排序
    Sublime Text3汉化好的绿色免安装版使用和破解教程+下载链接
    复杂数据类型(signal)的解读-C语言基础
    C/C++命名规范-C语言基础
    fgets()函数的详解以及使用时需要注意的一些细节-C语言基础
    scanf()函数的详解以及使用时需要注意的一些细节-C语言基础
    getchar()函数的详解以及使用时需要注意的一些细节-C语言基础
    如何用算法把一个十进制数转为十六进制数-C语言基础
    杨辉三角形实现过程详解-C语言基础
  • 原文地址:https://www.cnblogs.com/johnwonder/p/1661531.html
Copyright © 2011-2022 走看看