现在的位置: 首页 > 综合 > 正文

asp.net自定义配置节

2013年10月05日 ⁄ 综合 ⁄ 共 1082字 ⁄ 字号 评论关闭

asp.net自定义配置节

<configSections>
 <sectionGroup name="xiaos">
  <section name="xiao1" type="com.dyh.Fi,dyh"></section> 
/**注意:这里的type=","里的dyh是com.dyh.Fi所在的Assemble而不是命名空间**/ 
 </sectionGroup>
 
  </configSections> 
 
  <xiaos>
 <xiao1>
  <add key="yaya" value="gaga"></add>
 
 </xiao1>
 
  </xiaos>

 

public class Fi:System.Configuration.NameValueFileSectionHandler
 {   

 }

注意如果直接用:
<section name="SourceViewer" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </configSections>
后面的版本号等等参数一定不能省略。

后台处理:
using System.Collections.Specialized;

NameValueCollection ha=(NameValueCollection)System.Configuration.ConfigurationSettings.GetConfig("xiaos/xiao1");
   Response.Write(ha["yaya"]); 

.net规定,所有能够处理配置节的类必须要实现IConfigurationSectionHandler接口,而IConfigurationSectionHandler接口很简单,只有一个object Create(object parent,object configContext,XmlNode section)方法,这个方法不需要主动调用,它是在ConfigurationSettings.GetConfig这个静态方法的时候自动调用的,也就是说,当你在程序中使用ConfigurationSettings.GetConfig来获取配置节的时候,.net会根据改配置节声明中所定义的类名和路径自动实例化配置节处理类,并调用Create方法。但是你可以覆写create方法,也可以不覆写,直接调用getConfig即可。 

抱歉!评论已关闭.