类库探源——System.Configuration 配置信息处理
按照MSDN描述 System.Configuration 命名空间 包含处理配置信息的类型
本篇文章主要两方面的内容
1. 如何使用ConfigurationManager 读取AppSetting和ConnectionStrings
2. 如何使用自定义 Section,我这里的自定义Section格式为
<SectionName>
<services>
服务1的描述信息,供IoC容器使用
服务2的描述信息,供IoC容器使用
。。。
</services>
</SectionName>
其实如果读者使用一些比较出名的IoC 框架(Unity)都会有配置管理,根本不用自定义Section,但是技多不压身,多了解点东西总是没错的。
一、ConfigurationManager 类
提供对客户端应用程序配置文件的访问。无法继承此类
命名空间: System.Configuration
程序集: System.Configuration.dll
继承关系:

原型定义:public static class ConfigurationManager
由上面的定义可以看出ConfigurationManager是一个静态类
静态属性:
1. ConfigurationManager.AppSettings 获取当前应用程序默认配置的 AppSettingsSection 数据
原型定义:public static NameValueCollection AppSettings { get; }
从上面的定义可以看出 AppSetting 是一个 键值对
2. ConfigurationManager.ConnectionStrings 获取当前应用程序默认配置的 ConnectionStringsSection 数据
例子:
app.config
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v2.0.50727"/>
</startup> <connectionStrings>
<add name="KKSEntities" connectionString="metadata=res://*/DbModel.csdl|res://*/DbModel.ssdl|res://*/DbModel.msl;provider=System.Data.SqlClient;provider connection string="data source=XXX.XXX.XXX.XXX;initial catalog=KKS;user id=XXXX;password=KKKKK;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings> <appSettings>
<add key="Aphasia" value="www.cnblogs.com/Aphasia"/>
</appSettings>
</configuration>
Program.cs
using System;
using System.Configuration; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string web = ConfigurationManager.AppSettings["Aphasia"];
Console.WriteLine(web); string cnstr = ConfigurationManager.ConnectionStrings["KKSEntities"].ConnectionString;
Console.WriteLine(cnstr); }
}
}
效果:

二、 自定义Section
app.config
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="ServicesSection" type="CustomDemo.ServicesSection,CustomDemo"/>
</configSections> <ServicesSection>
<services>
<add ServiceName="XXX服务1" Impl="XXX.Impl.XXXService1" Inter="XXX.Inter.XXXInter1" />
<add ServiceName="XXX服务2" Impl="XXX.Impl.XXXService2" Inter="XXX.Inter.XXXInter2" />
<add ServiceName="XXX服务3" Impl="XXX.Impl.XXXService3" Inter="XXX.Inter.XXXInter3" />
</services>
</ServicesSection> <startup>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>
Program.cs
using System;
using System.Configuration; namespace CustomDemo
{
class Program
{
static void Main(string[] args)
{
ServicesSection myServices = ConfigurationManager.GetSection("ServicesSection") as ServicesSection;
foreach (Service item in myServices.ServiceItems)
{
Console.WriteLine("ServiceName:{0} Impl:{1} Inter:{2}", item.ServiceName, item.Impl, item.Inter);
}
}
} #region[自定义Section处理代码]
public class Service : ConfigurationElement
{
#region[当遇不能识别的元素时不让程序报错]
protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
{
return true;
}
protected override bool OnDeserializeUnrecognizedElement(string elementName, System.Xml.XmlReader reader)
{
return true;
}
#endregion #region[节点元素]
[ConfigurationProperty("ServiceName", IsRequired = true)]
public string ServiceName
{
get { return this["ServiceName"].ToString(); }
} [ConfigurationProperty("Impl", IsRequired = true)]
public string Impl
{
get { return this["Impl"].ToString(); }
} [ConfigurationProperty("Inter", IsRequired = true)]
public string Inter
{
get { return this["Inter"].ToString(); }
}
#endregion
} public class Services : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new Service();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Service)element).ServiceName;
}
} public class ServicesSection : ConfigurationSection
{
[ConfigurationProperty("services", IsDefaultCollection = false)]
public Services ServiceItems { get { return (Services)base["services"]; } }
}
#endregion
}
效果:

本小节代码下载
三、参考资料
类库探源——System.Configuration 配置信息处理的更多相关文章
- 类库探源——System.Delegate
一.MSDN 描述 Delegate 类:表示委托,委托是一种数据结构,它引用静态方法或引用类实例及该类的实例方法.(是不是感觉很像C语言中的函数指针 :) ) 命名空间: System 程序集: ...
- 类库探源——System.Exception
一.MSDN描述 Exception 类: 表示在应用程序执行期间发生的错误 命名空间 : System 程序集: mscorlib.dll 继承关系: 常用属性(含字段)和方法: 1. 属性Me ...
- 类库探源——System.Type
一.MSDN 描述 Type 类:表示类型声明:类类型.接口类型.数组类型.值类型.枚举类型.类型参数.泛型类型定义.以及开放或封闭构造的泛型类型. 命名空间: System 程序集:mscorlib ...
- 类库探源——System.Drawing.Bitmap
一.System.Drawing.Bitmap Bitmap 类: 封装GDI+ 位图,此位图由图形图像及其属性的像素数据组成.Bitmap 是用于处理由像素定义的图像的对象 命名空间: System ...
- 类库探源——System.Drawing
一.System.Drawing 命名空间简述 System.Drawing 命名空间提供访问 GDI+ 的基本功能,更高级的功能在 System.Drawing.Drawing2D,System.D ...
- 类库探源——System.Math 和 Random
一.System.Math Math类:为三角函数.对数函数和其他通用数学函数提供常数和静态方法 命名空间: System 程序集 : mscorlib.dll 继承关系: 常用属性: Math. ...
- 类库探源——System.ValueType
一.MSDN描述 ValueType 类:提供值类型的基类 命名空间: System 程序集: mscorlib.dll 继承关系: 值类型包括:字符.整数.浮点.布尔.枚举.结构(其实字符.整数 ...
- 类库探源——System.Environment
Environment 类: 提供有关当前环境和平台的信息以及操作它们的方法.此类不能被继承. 命名空间: System 程序集: mscorlib.dll 继承关系: 常用属性(字段)和方法: ...
- 类库探源——System.String
一.MSDN描述 String 类: 表示文本,即一系列的 Unicode 字符 命名空间 : System 程序集 : mscorlib.dll 继承关系: 备注: 1. 字符串是 Unicode ...
随机推荐
- IT小说
最近迷上了IT小说,连着读了好几个连载.伴随着一个项目的一些事,一些矛盾,也能体现出一个社会的缩影.最吸引的应该是一种熟悉感,常常想要是拍成电视剧也应该很好看,像<半泽植树>似的.看完了, ...
- 洛谷P1126 机器人搬重物
洛谷1126 机器人搬重物 题目描述 机器人移动学会(RMI)现在正尝试用机器人搬运物品.机器人的形状是一个直径1.6米的球.在试验阶段,机器人被用于在一个储藏室中搬运货物.储藏室是一个N*M的网格, ...
- Clean Code – Chapter 5 Formatting
The Purpose of Formatting Code formatting is about communication, and communication is the professio ...
- mac os的android开发国内库下载地址
http://ubuntu.buct.edu.cn/android/repository/
- MongoDB 入门之查询(find)
MongoDB 入门之查询(find) 1. find 简介 (1)find的第一个参数决定了要返回哪些文档. 空的查询文档会匹配集合的全部内容.默认就是{}.结果将批量返回集合c中的所有文档. db ...
- hdu 2711&&poj2182 Lost Cows (线段树)
从后往前查第一个为0的奶牛肯定应该排在第一个.每次从后往前找到第一个为0的数,这个数应该插在第j位.查找之后,修改节点的值为极大值,当整棵树的最小值不为0的时候查找结束. 至于这种查找修改的操作,再没 ...
- 1514:数值的整数次方 @jobdu
题目1514:数值的整数次方 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:377 解决:103 题目描述: 给定一个double类型的浮点数base和int类型的整数exponent. ...
- freemarker 数字,字符的操作
1. 数据类型转换: ${xx?string} //字符串 ${xx?number}//整数 ${xx?currency}//小数 ${xx?percent}//百分比 2. 截取字符串长度 有的时候 ...
- Java-Web监听器
在WEB端实现监听实质: 实现一系列的监听接口(实现相应的接口,覆写各接口中相应的方法,在相应的事件触发的时候会执行自己的监听器中的覆写的方法,在各个方法中完成自己想要的操作,从而实现了监听) 监听- ...
- tomcat热部署,更改java类不用重新加载context
修改类后,tomcat热部署会重新加载整个项目的context,影响开发效率.网上查的大多数是将server的modules标签中Auto Reload项改为Disabled,但是没有效果. 使用以下 ...