老实说,在以前没写个自定义配置节点之前,我都是写到一个很常用的节点里面,就是appSettings里add,然后再对各个节点的value值进行字符串分割操作,根据各种分割字符嵌套循环处理,后来看到一些常用的框架都会加个configSections,然后百度后发现可以自己写配置文件节点,然后就在项目中定义自己的节点

  其实,上面说的都是废话,现在项目中要用WCF服务,用传统的WCF配置工具或者手写的会有一堆配置,所以我稍作了简化,看下面的

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="test" type="TestService.Common.Config.TestServiceSection, TestService.Common" />
</configSections>
<test>
<add name="TestService" service="TestService.ApplicationService.TestService, TestService.ApplicationService"
contract="TestService.Contract.ITestService, TestService.Contract" uri="net.tcp://localhost:10001/TestService" />
<add name="FileService" service="TestService.ApplicationService.FileService, TestService.ApplicationService"
contract="TestService.Contract.IFileService, TestService.Contract" uri="net.tcp://localhost:10001/FileService" />
<add name="MessageService" service="TestService.ApplicationService.MessageService, TestService.ApplicationService"
contract="TestService.Contract.IMessageService, TestService.Contract" uri="net.tcp://localhost:10001/MessageService" />
</test>
</configuration>

  上面的test就是我定义的节点,子节点是各个WCF的配置,看下TestServiceSection里面是个什么鬼

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text; namespace TestService.Common.Config
{
public sealed class TestServiceSection : ConfigurationSection
{
private static readonly ConfigurationProperty s_property = new ConfigurationProperty(string.Empty, typeof(TheKeyValueCollection), null,
ConfigurationPropertyOptions.IsDefaultCollection); [ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
public TheKeyValueCollection KeyValues
{
get
{
return (TheKeyValueCollection)base[s_property];
}
} public static TestServiceSection GetConfig()
{
TestServiceSection configSection= (TestServiceSection)ConfigurationManager.GetSection("test");
if (configSection == null)
throw new ConfigurationErrorsException("Section test is not found.");
return configSection;
} public static TestServiceSection GetConfig(string configPath)
{
var fileMap = new ExeConfigurationFileMap() { ExeConfigFilename = configPath };
var config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
TestServiceSection configSection = (TestServiceSection)config.GetSection("test");
if (configSection == null)
throw new ConfigurationErrorsException("Section test is not found.");
return configSection;
}
} [ConfigurationCollection(typeof(TheKeyValue))]
public class TheKeyValueCollection : ConfigurationElementCollection // 自定义一个集合
{
new TheKeyValue this[string name]
{
get
{
return (TheKeyValue)base.BaseGet(name);
}
} // 下面二个方法中抽象类中必须要实现的。
protected override ConfigurationElement CreateNewElement()
{
return new TheKeyValue();
} protected override object GetElementKey(ConfigurationElement element)
{
return ((TheKeyValue)element).Name;
}
} public class TheKeyValue : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get { return this["name"].ToString(); }
set { this["name"] = value; }
} [ConfigurationProperty("uri", IsRequired = true)]
public string Uri
{
get { return this["uri"].ToString(); }
set { this["uri"] = value; }
} [ConfigurationProperty("service", IsRequired = true)]
public string Service
{
get { return this["service"].ToString(); }
set { this["service"] = value; }
} [ConfigurationProperty("contract", IsRequired = true)]
public string Contract
{
get { return this["contract"].ToString(); }
set { this["contract"] = value; }
}
}
}

  有了上面的一块代码,编译是冒得问题的,那么问题来了,怎么取自定义配置节点里的东西呢?

  look

TestServiceSection services = TestServiceSection.GetConfig();
foreach (TheKeyValue item in services.KeyValues)
{
var i = item.ElementInformation; }

  循环里面的就是配置节点里的信息,可以卡个断点看看,是不是so easy!好了,今天就写到这儿.

C#自定义配置文件节点的更多相关文章

  1. springboot读取自定义配置文件节点

    今天和大家分享的是自定义配置信息的读取:近期有写博客这样的计划,分别交叉来写springboot方面和springcloud方面的文章,因为springboot预计的篇章很多,这样cloud的文章就需 ...

  2. C#自定义配置文件节的实现

    1.配置文件:(注意configSections必须放在最上面否则会报错) <?xml version="1.0" encoding="utf-8" ?& ...

  3. C#自定义配置文件(一)

    C#自定义配置文件 .NET程序中,经常使用Config文件来配置应用程序中经常使用的值,比如数据库连接字符串.最近项目遇到一个需要配置好多节点在配置文件中的需求.为了使配置节点整洁易维护,在代码调用 ...

  4. .Net 配置文件--继承ConfigurationSection实现自定义处理类处理自定义配置节点

    除了使用继承IConfigurationSectionHandler的方法定义处理自定义节点的类,还可以通过继承ConfigurationSection类实现同样效果. 首先说下.Net配置文件中一个 ...

  5. .Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点

    除了使用继承IConfigurationSectionHandler的方法定义处理自定义节点的类,还可以通过继承ConfigurationSection类实现同样效果. 首先说下.Net配置文件中一个 ...

  6. App.config和Web.config配置文件的自定义配置节点

    前言 昨天修改代码发现了一个问题,由于自己要在WCF服务接口中添加了一个方法,那么在相应调用的地方进行更新服务就可以了,不料意外发生了,竟然无法更新.左查右查终于发现了问题.App.config配置文 ...

  7. VS2012 常用web.config配置解析之自定义配置节点

    在web.config文件中拥有一个用户自定义配置节点configSections,这个节点可以方便用户在web.config中随意的添加配置节点,让程序更加灵活(主要用于第三方插件的配置使用) 自定 ...

  8. C#创建自定义配置节点

    转载:http://www.educity.cn/develop/495003.html 在.Net应用程序中我们经常看到VS为我们生成的项目工程中都会含有app.config或者web.connfi ...

  9. Spring Boot2.0自定义配置文件使用

    声明: spring boot 1.5 以后,ConfigurationProperties取消locations属性,因此采用PropertySource注解配合使用 根据Spring Boot2. ...

随机推荐

  1. Echarts Jqplot嵌extjs4 windows 装配方法

    js组件绘图终于是画在一个指定id的div或dom元素中. 在项目中有可能须要画在 Extjs容器中,研究了一下,能够通过下面的思路实现,方法跟大家共享下: 1.首先做一个容器,把此内容加入到wind ...

  2. Skynumber

    Time Limit: 1000ms Memory Limit: 128000KB 64-bit integer IO format:      Java class name: Submit Sta ...

  3. XP下类似%windir% %userprofile% 的变量的说明(转)

    在一些批处理或者系统技巧操作教程文章中,我们常常会看到一些形如 %windir% 或者 %systemdrive% 的变量.这些变量都代表着什么含义呢?下面小技巧之家为大家整理了在Windows XP ...

  4. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(4)-业务逻辑层的封装

    原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(4)-业务逻辑层的封装 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   (1):框架搭建    (2) ...

  5. 浅谈 js 正则字面量 与 new RegExp 执行效率

    原文:浅谈 js 正则字面量 与 new RegExp 执行效率 前几天谈了正则匹配 js 字符串的问题:<js 正则学习小记之匹配字符串> 和 <js 正则学习小记之匹配字符串优化 ...

  6. hdu 游乐场

    Problem Description   小时候,因为家里经济困难,小明从未去过游乐场,所以直到现在,他还心存遗憾.  最近,杭州刚建了一座游乐场,为了弥补儿时的遗憾,小明带了一笔钱迫不及待地要去体 ...

  7. 瘸腿蛤蟆笔记29-cocos2d-x-3.2 Box2d物理引擎dynamics模块介绍

    转载标明出处:http://blog.csdn.net/notbaron/article/details/38611335 上篇回想 本篇名言:奋斗.寻觅.发现,而不屈服.[诗人丁尼生] 上篇中,我们 ...

  8. LayOutControl

    DevExpress DXperience 12.2 在 Navigation & Layout 中 有个 LayOutControl 它适用于做布局,我们普通控件 长宽 只能给固定的值,这个 ...

  9. The Swift Programming Language-官方教程精译Swift(8)闭包 -- Closures

    闭包是功能性自包含模块,可以在代码中被传递和使用. Swift 中的闭包与 C 和 Objective-C中的 blocks 以及其他一些编程语言中的 lambdas 比较相似. 闭包可以捕获和存储其 ...

  10. 用windows性能监视器监控sqlserver的常见指标

    用windows性能监视器监控sqlserver的常见指标   上边文章中提到win的性能监视器是监控数据库性能必备的工具,接下来我就给大家介绍一些常见的监控指标,其实无非就是磁盘,cpu,内存等硬件 ...