自定义配置app.config

本文介绍了一种使用.NET Framework自定义配置文件的方法。通过创建自定义配置节类,可以灵活地管理应用程序配置。示例展示了如何定义DbFactorySection、DefaultElement等类,并在app.config中应用这些自定义配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先要写配置类,然后封装成DLL
ExpandedBlockStart.gif 代码
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;
            }
        
        }

    }
}

 

ExpandedBlockStart.gif 代码
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;
            }
        
        }
    }
}

 

 

ExpandedBlockStart.gif 代码
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;
            }
        }
           
    }
}

 

 

ExpandedBlockStart.gif 代码
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中加入以下节点:

 

ExpandedBlockStart.gif 代码
< 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

转载于:https://www.cnblogs.com/johnwonder/archive/2010/02/02/1661531.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值