IConfigurationSectionHandler 接口的用法

对asp.net 的配置文件即熟悉又陌生,熟悉的是天天在使用,陌生是实现原理还是个糊涂的概念.
今天终于花了点时间了解一下IConfigurationSectionHandler 接口的用法 ,引以入门.
首先建立一 RobsunConfigSectionHandler 专案,代码如下 :
None.gif
None.gif
namespace  RobsunConfigSectionHandler
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class RobsunParaSectionHandler:IConfigurationSectionHandler
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IConfigurationSectionHandler 成員#region IConfigurationSectionHandler 成員
InBlock.gif
InBlock.gif        
public object Create(object parent, object configContext, XmlNode section)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            RobsunPara para 
= new RobsunPara();
InBlock.gif
InBlock.gif            
foreach (XmlNode xn in section.ChildNodes)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
switch (xn.Name)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
case "path":
InBlock.gif                        para.Path 
= xn.SelectSingleNode("@value").InnerText;
InBlock.gif                        
break;
InBlock.gif                    
case "companyName":
InBlock.gif                        para.CompanyName 
= xn.SelectSingleNode("@value").InnerText;
InBlock.gif                        
break;
InBlock.gif                    
case "isPrivate":
InBlock.gif                        para.IsPrivate 
= bool.Parse(xn.SelectSingleNode("@attribute").InnerText);
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return para;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
RobsunParaSectionHandler类实现 IConfigurationSectionHandler 接口.

RobsunPara类(实际就是参数类)的代码如下:
None.gif
None.gif    
public   class  RobsunPara
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
ContractedSubBlock.gifExpandedSubBlockStart.gif        
code#region code
InBlock.gif        
private string _path = "";
InBlock.gif        
private string _companyName = "";
InBlock.gif        
private bool _isPrivate = false;
InBlock.gif
InBlock.gif        
public string Path
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _path; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _path = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string CompanyName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _companyName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _companyName = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public bool IsPrivate
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _isPrivate; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _isPrivate = value; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedBlockEnd.gif    }

ConfigurationHander类的创建工作已完成,次handler的工作必须配合如下配置文件的格式(其实的配置文件依赖handler).这里是web.config(三种配置文件任一,web.config,App.config,machine.config ,前两种会覆盖machine.config,当然这里是没法子在App.config配置了 ~Q~ )
< configuration >
  
    
< configSections >
        
< sectionGroup  name ="robsunGroup" >
            
< section  name ="robsunSection"  type ="RobsunConfigSectionHandler.RobsunParaSectionHandler,RobsunConfigSectionHandler" />
        
</ sectionGroup >
    
</ configSections >
  
    
< robsunGroup >
        
< robsunSection >
            
< path  value ="NONE" />
            
< companyName  value ="Robsun" />
            
< isPrivate  attribute ="true" />
        
</ robsunSection >
    
</ robsunGroup >
  
    
< appSettings />
    
< connectionStrings />
    
< system .web >
    ...
准备代码都已完成.剩下的就是拿来用了....
这里为了简单起见,就直接在page_load里实现了:
None.gif
None.gif
public  partial  class  _Default : System.Web.UI.Page 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        RobsunConfigSectionHandler.RobsunPara para 
InBlock.gif            
= ConfigurationManager.GetSection("robsunGroup/robsunSection"as RobsunConfigSectionHandler.RobsunPara;
InBlock.gif
InBlock.gif        Response.Write(
"CompanyName: " + para.CompanyName + "<br>");
InBlock.gif        Response.Write(
"Path: " + para.Path + Environment.NewLine + "<br>");
InBlock.gif        Response.Write(
"isPrivate: " + para.IsPrivate.ToString() + "<br>");
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

以上只是对IConfigurationSectionHandler 接口的简单事例.配置文件的强大也只有在以后的工作中慢慢体会了.

转载于:https://www.cnblogs.com/Robsun/archive/2007/01/18/624040.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值