C#自定义配置文件节点

时间:2023-03-09 00:15:12
C#自定义配置文件节点

  老实说,在以前没写个自定义配置节点之前,我都是写到一个很常用的节点里面,就是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!好了,今天就写到这儿.