并且,1.1中的配置文件对于程序来说是只读的,若配置文件一旦发生改变怎会引发应用程序重启。
而现在,2.0提供了全新的自定义配置文件构建方式,支持强类型的配置属性,并提供了代码对配置文件进行动态修改的机制。我对其作了一些初步的研究,在这里抛砖引玉了。
- 首先我们来看看最简单的情况如何处理:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="RemotingCompress" type="Xrinehart.Framework.CommSupport.Section.RemotingCompressSection, ConfigManagement"
allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true"/>
</configSections>
<RemotingCompress CompressLevel="DEFAULT_COMPRESSION" CompressCacheSize="1024" UnCompressCacheSize="1024" MinCompressSize="0">
</configuration>只有一个配置节:RemotingCompress,拥有4个属性CompressLevel、CompressCacheSize、UnCompressCacheSize、MinCompressSize。
我们需要实现一个从System.Configuration.ConfigurationSection基类继承的配置类,在2.0里配置信息实体和配置识别都在一个类中,我们先来看代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace Xrinehart.Framework.CommSupport.Section
{
/**//// <summary>
/// 压缩率级别的枚举
/// </summary>
public enum Deflater
{
/**//// <summary>
/// 最佳压缩效果, 速度慢
/// </summary>
BEST_COMPRESSION = 9,
/**//// <summary>
/// 默认压缩率, 压缩速度比最高(推荐)
/// </summary>
DEFAULT_COMPRESSION = -1,
/**//// <summary>
/// 强化压缩率,速度较慢
/// </summary>
DEFLATED = 8,
/**//// <summary>
/// 最佳速度压缩率,速度最快(宽带推荐)
/// </summary>
BEST_SPEED = 1,
/**//// <summary>
/// 不压缩
/// </summary>
NO_COMPRESSION =0
};
/**//// <summary>
/// RemotingCompress配置节类
/// </summary>
public sealed class RemotingCompressSection : System.Configuration.ConfigurationSection
{
// 该配置文件只读
private static bool _ReadOnly = true;
public RemotingCompressSection()
{
}
private new bool IsReadOnly
{
get
{
return _ReadOnly;
}
}
private void ThrowIfReadOnly(string propertyName)
{
if (IsReadOnly)
throw new ConfigurationErrorsException(
"The property " + propertyName + " is read only.");
}
protected override object GetRuntimeObject()
{
// To enable property setting just assign true to
// the following flag.
_ReadOnly = true;
return base.GetRuntimeObject();
}
所有配置属性#region 所有配置属性
/**//// <summary>
/// 配置节属性:压缩率级别(可选)