C#如何使用和开发自定义配置节

在日常的程序设计中,如何灵活和巧妙地运用配置信息是一个成功的设计师的首要选择。这不仅是为了程序设计得更灵活性和可扩展性,也是为了让你的代码给人以清新的感觉。程序中的配置信息一般放在应用程序的app.config或web.config文件中,当然也可以自定义自己的配置文件。这些配置文件是以XML格式进行存储和读取的。微软也封装一些对这些配置文件操作的类,这些类存在于名字空间System.Configuration下,这个命名空间包含提供用于处理配置数据的编程模型的类型,当然为了使用还添加System.Configuration.dll程序集。

现在我们先看一下这个名字空间下的几个重要的类:

1、ConfigurationManager,这个提供用于打开客户端应用程序集的Configuration对象。

2、WebConfigurationMaManager,这个提供用于打开web应用程序集的Configuration对象。

3、ConfigurationSection ,表示配置文件中的节。

4、ConfigurationSectionCollection ,表示配置文件中相关节的集合。

5、ConfigurationSectionGroup ,表示配置文件中的一组相关节。

6、ConfigurationSectionGroupCollection ,表示 ConfigurationSectionGroup 对象的集合。

7、ConfigurationProperty ,表示属性或配置元素的子元素。

8、ConfigurationPropertyAttribute ,以声明方式指示 .NET Framework,以实例化配置属性。

9、ConfigurationElement ,表示配置文件中的配置元素。

10、ConfigurationElementCollection ,表示包含一个子元素集合的配置元素。

当然这里面这常用的是ConfigurationManager类,这个类提供了两个静态常用的静态方法:

object GetSection(string sectionName)用于读取当前应用程序默认配置的指定配置信息;

Configuration OpenExeConfiguration(ConfigurationUserLevel userLevel)将当前应用程序配置文件打开以得到一个Configuration对象。

以及两个静态属性:AppSettins获取当前应用程序默认配置的AppSettingsSection数据;ConnectionStrings获取当前应用程序默认配置的ConnectionStringSection数据。

当然由于AppSettings是一个NameValueCollection对象,因此对它的读取和设置可以直接以AppSettings[“mySet”]的形式得到,相应的在*.config文件中的<appsettings>节下添加<add name=”mySet” value=””/>即可。

要自定义配置节配置元素,就必须要使我们的类分别继承自ConfigurationSection和ConfigurationElement类。那么实现这个两个类的对象就可以加入到配置文件的<configSection>和其他元素节点中。

这儿介绍在配置节点中加入自定义配置信息的能力(元素节点)。

1、实现一个继承自ConfigurationElement和ConfigurationSection的类,并添加的配置属性加上ConfigurationPropertyAttribute特性。

2、添加配置信息。

3、在程序中读取配置信息。

第一步:实现派生自ConfigurationSection和ConfigurationElement的类

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

-->using System;

using System.Collections;

using System.Text;

using System.Configuration;

using System.Xml;

namespace MyCustomConfiguration

{

//自定义配置节

public class CustomSectionConfiguration : ConfigurationSection

{

public CustomSectionConfiguration() { }

public CustomSectionConfiguration(string value) { }

//添加特性ConfigurationPropertyAttribute

//‘customAttribute‘是配置文件中的元素

//‘CustomAttribute‘是程序中要的属性;

[ConfigurationProperty("customAttribute", DefaultValue = "", IsRequired = true)]

public string CustomAttribute

{

get { return (string)base["customAttribute"]; }

set { base["customAttribute"] = value; }

}

//添加特性ConfigurationPropertyAttribute

[ConfigurationProperty("customElement", DefaultValue = "", IsRequired = true)]

public CustomElementConfiguration CustomElement

{

get { return (CustomElementConfiguration)base["customElement"]; }

set { base["customElement"] = value; }

}

}

//自定义配置元素

public class CustomElementConfiguration : ConfigurationElement

{

public CustomElementConfiguration() { }

public CustomElementConfiguration(string value1, string value2)

{

Value1 = value1;

Value2 = value2;

}

[ConfigurationProperty("value1", DefaultValue = "", IsRequired = true)]

public string Value1

{

get { return (string)base["value1"]; }

set { base["value1"] = value; }

}

[ConfigurationProperty("value2", DefaultValue = "", IsRequired = true)]

public string Value2

{

get { return (string)base["value2"];}

set { base["value2"] = value; }

}

}

}

第二步:在*.config中设置自定的元素

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><configuration> 

 <!-- 配置节-->
   <configSections>
     <sectionGroup name="customGroup">
       <section
         name="customSection"
         type="

 MyCustomConfiguration.CustomSectionConfiguration, MyCustomConfiguration, Version=1.0.0.0, Culture=neutral, 

 PublicKeyToken=null"   allowLocation="true"    allowDefinition="everywhere"  />
     </sectionGroup>

 ……
   <!-- 配置节设置信息 -->
   <customGroup>
     <customSection customAttribute="Custom">
       <customElement Value1=”best" Vaule2=”better”/>
     </customSection>
   </customGroup> 

 ……

 </configuration>

第三步:在程序中使用配置信息。

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

-->MyCustomConfiguration.CustomSectionConfiguration config = (MyCustomConfiguration.CustomSectionConfiguration)System.Configuration.ConfigurationManager.GetSection( "customGroup/customSection");

首先得到配置节对象:         接着就可以使用强名称的对象和属性了。

上面介绍的是单一属性的配置,如果要配置多个对象,那么就得使用System.Configuration.ConfigurationElementCollection,这类的作用是生成多个子对象,也就是在一个标签下可以放置多个System.Configuration.ConfigurationElement对象。

方法同上,我们必须要重写几方法:

ConfigurationElement CreateNewElement()//这个方法的作用是返回子对象实例;

object GetElementKey(ConfigurationElement element);//这个方法的得到对象中的键名;

ConfigurationElementCollectionType CollectionType{get;}//这个属性是定义映射方式;

string ElementName{get;}//这个属性是定义XML元素的名字。

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

-->protected override ConfigurationElement CreateNewElement()

{

return new CustomElementConfiguration ();

}

protected override object GetElementKey(ConfigurationElement element)

{

return ((CustomElementConfiguration )element).Name;

}

public override ConfigurationElementCollectionType CollectionType

{

get { return ConfigurationElementCollectionType.BasicMap; }

}

protected override string ElementName

{

get { return "collection"; }

}

时间: 2024-08-29 13:35:22

C#如何使用和开发自定义配置节的相关文章

基于Spring的可扩展Schema进行开发自定义配置标签支持

一.背景 最近和朋友一起想开发一个类似alibaba dubbo的功能的工具,其中就用到了基于Spring的可扩展Schema进行开发自定义配置标签支持,通过上网查资料自己写了一个demo.今天在这里进行和大家分享,也记录下方便以后复习备忘. 二.demo测试环境 1.JDK1.7 2.spring 4.2.5.RELEASE 3.基于Maven 4.开发工具Eclipse 三.项目介绍 1.实现步骤分析 [1].设计配置属性并开发JavaBean. [2].编写xsd文件. [3].编写Nam

自定义配置节与配置节的读取

一.引子 你是否也遇到过这样的问题:项目很多配置都写到了App.Config或Web.Config的AppSettings内,每个人都加了几条,到最后囤积了大量的配置,分不清哪个是有用的.哪个是没用的了.(即便加了相关注释,也是乱的可以) 1   <appSettings>  2     <!--是否抛出异常-->  3     <add key="HasException" value="true" />  4     <

ASP.NET添加和读取Web.Config自定义配置节

自定义节 1.首先在<configSections>中定义自定义配置节(例如Index.testSection)和对应的自定义配置节处理程序(例如NameValueSectionHandler) 2.然后添加节的内容 <configuration> <configSections> <sectionGroup name="Rewrite.NET"> <section name="Index" type="

在ASP.NET中创建自定义配置节(翻译)

2017年,共享经济持续成为大众关注的焦点,从共享单车.共享雨伞.共享充电宝,到共享电动车.共享汽车.共享床位,甚至连女友都拿来共享了.戴上"共享"高帽的创业项目一茬接一茬地冒出来,正如收割的韭菜,最开始两茬是最嫩的,接下来生长出来的则会让人觉得食之无味又弃之可惜.对于投资人如此,对于用户们来说有何尝不是呢? 让我们盘点下近一年出现过的"共享"明星们,对于它们,死亡还是生存?这是个问题. 据统计,2016年中国的共享经济市场规模接近4万亿元:2017年,共享系宣告进

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

主要代码,一定要继续System.Configuration.ConfigurationSection,具体的节点名称可以自行修改 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.WebC

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

核心代码 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

自定义配置节

原文地址:https://www.cnblogs.com/springsnow/p/9399286.html

【.net 深呼吸】自定义应用程序配置节

实际上,应用程序配置文件 App.config,是由各个节(Configuration Section)组成的,通常,配置节是按功能划分的,比如我们很熟悉的 appSettings.connectionStrings.startup.system.ServiceModel…… 在实际开发中,我们的应用程序也应该需要一个咱们程序专且功能的配置节,这样也方便我们在代码中通过相关的API来读取,而不是用普通的XML文件读取方法. 其实,实现自定义配置节并不难,只是,有几个问题要注意. 老周一边给大伙伴

spring boot--日志、开发和生产环境切换、自定义配置

Spring Boot日志常用配置: # 日志输出的地址:Spring Boot默认并没有进行文件输出,只在控制台中进行了打印 logging.file=/home/zhou # 日志级别 debug-> info -> warning -> error # 默认级别为 info # 如果设置了debug=true的时候,日志级别会自动降低为debug # ROOT代表默认全局设置 logging.level.ROOT=INFO # 可以设置指定包的输出级别,这样的话,指定的包,级别以下