核心代码

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;
}
}
} }

配置文件

  <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"]

ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节集合的更多相关文章

  1. ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节

    主要代码,一定要继续System.Configuration.ConfigurationSection,具体的节点名称可以自行修改 using System; using System.Data; u ...

  2. 使用 ConfigurationSection 创建自定义配置节

    我们可以通过用自己的 XML 配置元素来扩展标准的 ASP.NET 配置设置集,要完成这一功能,我们必须实现继承System.Configuration.ConfigurationSection 类来 ...

  3. C#创建自定义配置节

    在.Net应用程序中,我们经常看到VS为我们生成的项目工程中都会含有nfig或者nfig这样的文件.这个文件就是我们所说的应用程序配置文件.在这个文件里面记述着一些与我们的应用程序相关的信息,如:数据 ...

  4. 在ASP.NET项目中的web.config文件里配置数据库连接并在程序代码中获取连接字符串

      1.在<connectionStrings> 标签里添加连接 <connectionStrings> <add name="ConnectionName&q ...

  5. asp.net项目中通过Web.config配置文件及文件夹的访问权限!

    描述:在开发中我们通常会碰到这样的问题,例如:在项目的根目录下面有一个文件或者文件夹需要用户登陆后才能访问.如果用户在没有登录的情况下访问该文件或者该文件夹下面的文件时,直接拦截重定向到对应的登陆页面 ...

  6. IOC注入框架——Unity中Web.Config文件的配置与调用

    Unity 应用程序块可以从 XML 配置文件中读取配置信息.配置文件可以是 Windows Forms 应用程序的 App.config 或者 ASP.NET 应用程序的 Web.config.当然 ...

  7. Unity中Web.Config文件的配置与调用

    在上一篇文章“Unit简单依赖注入”我们可以实现构造对象和被依赖对象之间的 松耦合,使我们的抽象层(Player)能够保持稳定,但是在并没有把客户类和Player类之间彻底解耦,即当我们不想使用MP3 ...

  8. web.config文件中配置数据库连接的两种方式

    web.config文件中配置数据库连接的两种方式 标签: 数据库webconfig 2015-04-28 18:18 31590人阅读 评论(1)收藏举报    分类: 数据库(74)  在网站开发 ...

  9. 如何在web.config文件中配置Session变量的生命周期

    实例说明:在网上购物商城中,为了维护在线购物环境,一般只有注册会员才可以购买商品.实现购物功能时,先通过Session变量记录会员的登录名,然后在购买商品页面通过判断会员是否登录确定其能否购买商品. ...

随机推荐

  1. UGUI&&Animator模块知识点随记

    1.Render Texture,把这个赋给摄像机,这个对象就保存了摄像机拍摄到的纹理,再把他赋给Raw Image. 2.给Button添加事件关联时,函数不能带有yield WaitForSeco ...

  2. 2.springMVC+spring+Mybatis整合

    前面已经说了,springMVC+spring+Mybatis的整合思路了,现在就照着这个思路来做一下: 在开始之前先来看一下工程的目录结构: config这个目录底下放的都是配置文件: mybati ...

  3. cf 731f

    题意:给你一组数,让你先选一个数,然后把其他数缩小成他的倍数,然后求和,求最大值. 思路:很显然,对于数i,在i*j到i*(j+1)-1之间的数都应该缩小成i*j,又由于数均小于等于200000这个通 ...

  4. 关于GC的几篇文章

    http://msdn.microsoft.com/zh-cn/magazine/bb985010(en-us).aspx http://msdn.microsoft.com/zh-cn/magazi ...

  5. 比较两个NSDate类型的参数相差的时间差

    +(NSInteger)getDaysFrom:(NSDate *)serverDate To:(NSDate *)endDate { NSCalendar *gregorian = [[NSCale ...

  6. 解决Linux系统下程序找不到动态库的方法

    思路:一般来说,通过make命令已经将程序依赖的动态库编译出来了,通过make install命令已经将动态库安装到系统的某个路径下.找没找到动态库就看这个路径是否包含在系统默认搜索动态库的路径中,如 ...

  7. textarea之placeholder

    placeholder 属性适用于以下的 <input> 类型:text, search, url, telephone, email 以及 password.这个属性是html5才有的新 ...

  8. HTML5 ---localStorage储存实例

     <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title> ...

  9. Light OJ 1032 - Fast Bit Calculations(数学)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1032 题目大意:一个十进制数变化为二进制,那么对于这个数,如果连着两个二进制位 ...

  10. h5弹框去掉ip地址

    <script> window.alert = function(name){ var iframe = document.createElement("IFRAME" ...