博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节集合
阅读量:7198 次
发布时间:2019-06-29

本文共 2927 字,大约阅读时间需要 9 分钟。

核心代码

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

配置文件

代码中使用

DbFactorySection dfs = ConfigurationManager.GetSection("dbFactory") as DbFactorySection;FactoryElements fes = dfs.Factorys;FactoryElement feSql = fes["sql"];FactoryElement feSqlite = fes["sqlite"]

 

转载于:https://www.cnblogs.com/yonsy/p/5620580.html

你可能感兴趣的文章
SVN 学习
查看>>
SmartSVN设置ignoreList
查看>>
ios-网址中的中文或者非法字符转换
查看>>
白话SpringCloud | 第零章:前言
查看>>
XMind中的“任务信息”视图
查看>>
OSChina 双十一乱弹 ——来自单身狗的哀鸣
查看>>
OSChina 周三乱弹 ——我们职业更好的名字:爱码士
查看>>
左边的项目管理器不见了
查看>>
android 获取唯一标识
查看>>
HTML5 - Server-Sent Events
查看>>
网络研讨会的邀请:网络公开课_守护好数据库的备份信息
查看>>
使程序在Linux下后台运行
查看>>
关于赋值语句的一点看法
查看>>
windows版本的Emacs 无法显示图片的解决方法
查看>>
Discuz! 经典加密解密函数(带详解)
查看>>
JVM内存结构和6大区域
查看>>
centos6 Docker桥接到主机所在的内网
查看>>
C++ 动态内存
查看>>
网络安装CentOS5.5Final
查看>>
View requires API level 14 (current min is 8)
查看>>