问一个ConfigurationSection的问题(自定义配置节)

NqIceCoffee 2008-11-17 02:46:46
我现在只想获得我自己配置的节点对象XmlNode

如何做到?请教大家,50分送上
...全文
523 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
呵呵,我不是说你程序每次去解析xml,是你以后每新作一个section的时候,还要去写不同解析xml的代码,也要写一个对应你xml的类,否则别人用你的配置的话就会很费劲。
而我的做法,只要把原来的copy一份改改属性就可以了,你看我的代码,连获取seciton的方法都已经另外抽出来了。

不过个人有个人的习惯,这个话题到这里就可以了,嘿嘿。
NqIceCoffee 2008-11-17
  • 打赏
  • 举报
回复
回9楼的话

web.config的配置项肯定是全局的,所以肯定缓存起来了。一次读取就OK了

另外,如果我只是想实现一个很简单的配置节的话,实现三个类,我会感觉太。。。

所以更喜欢以自己的方式去实现它
  • 打赏
  • 举报
回复
你不会配置文件也经常要去修改它的属性吧?
这个东西写好一次以后就不要动了,麻烦什么呢?而且这种方式的好处就在于能定义复合节,而且是对象化的。使用起来方便。一次写好,其他使用的地方相当方便。

即使你要加属性,加个属性就可以了,对应配置文件里加个属性而已。
你那样做每次都要去调用GetSeciton还要解析xml。从编程的角度来说肯定没这个方便。
wjxluck 2008-11-17
  • 打赏
  • 举报
回复
mark
  • 打赏
  • 举报
回复
我刚刚贴的代码是完全自定义的,除了另外一个节是复合节,其他你可以直接拷贝过去,然后把属性改改就能用了。
当然命名空间也要改掉:)
NqIceCoffee 2008-11-17
  • 打赏
  • 举报
回复
2楼的朋友

我就是不想那么麻烦,实现那么多的类,去实现一个配置节,感觉上很累

我想得到当前的节点,然后直接读取XML节点生成为自己的自定义对象,仍然感谢
NqIceCoffee 2008-11-17
  • 打赏
  • 举报
回复
以前实现IConfigurationSectionHandler这个接口的时候

我只要用下面的方法,然后使用Configuration.GetSection("XXX")就的得到

public object Create(object parent, object configContext, XmlNode section)
{
return section;
}



但用了继承自ConfigurationSection后,试了好多方法,都不行- -

Configuration.GetSection("XXX")在使用这个的时候直接报找不到XXX节点 - -
  • 打赏
  • 举报
回复
这是个例子啊,不能直接用:)
  • 打赏
  • 举报
回复
/// <summary>
/// 配置管理类
/// </summary>
public static class ConfigurationManager
{
private const string CONFIGURATIONSIGN = "ConfigurationManager_CONFIGURATIONSIGNCOLLECTION";

private static ConfigurationSectionCollection _configurationSections;

/// <summary>
/// 所有sections集合
/// </summary>
public static ConfigurationSectionCollection Current
{
get
{
if (_configurationSections == null)
{
lock (CONFIGURATIONSIGN)
{
if (_configurationSections == null)
{
_configurationSections = GetConfigurationSectionCollection();
}
}
}
return _configurationSections;
}
}

private static ConfigurationSectionCollection GetConfigurationSectionCollection()
{
try
{
System.Configuration.Configuration cfg;
if (HttpContext.Current != null)
{
cfg = WebConfigurationManager.OpenWebConfiguration("~/web.config");
}
else
{
// Windows forms application
ExeConfigurationFileMap file = new ExeConfigurationFileMap();
Process process = Process.GetCurrentProcess();
string fileName = process.ProcessName;
file.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, string.Concat(fileName ,".exe.config"));
cfg = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
}
if (cfg == null)
{
throw new ConfigurationErrorsException("配置文件读取错误!");
}
return cfg.Sections;
}
catch (ConfigurationErrorsException configurationException)
{
throw new ConfigurationErrorsException(String.Format("读取配置文件出错:{0}", @"'ApplicationConfiguration'"), configurationException);
}
}
}
读取配置seciton的类
  • 打赏
  • 举报
回复
/// <summary>
/// <configSections>
/// <section name="cacheConfiguration" type="Skyline.Tools.CacheDrive.Configuration.CacheConfiguration, Skyline.Tools"/>
///</configSections>
/// <cacheConfiguration monitorSeparator ="1">
/// <syncCacheSettings>
/// <add name="" type="" syncSeparator="" />
/// </syncCacheSettings>
/// </cacheConfiguration>
/// </summary>
public sealed class CacheConfiguration : ConfigurationSection
{
/// <summary>
/// 当前缓存配置信息
/// </summary>
public static CacheConfiguration Current
{
get
{
CacheConfiguration cfg = (CacheConfiguration)Skyline.Tools.Configuration.ConfigurationManager.Current["cacheConfiguration"];
if (cfg == null)
{
throw new ConfigurationErrorsException("读取缓存配置信息错误!");
}
return cfg;
}
}

/// <summary>
/// 日志名称
/// </summary>
[ConfigurationProperty("monitorSeparator", IsRequired = false,DefaultValue="1")]
public int MonitorSeparator
{
get { return (int)this["monitorSeparator"]; }
set { this["monitorSeparator"] = value; }
}

/// <summary>
/// 同步缓存器配置
/// </summary>
[ConfigurationProperty("syncCacheSettings",IsRequired = false)]
public SyncCacheConfiguration SyncCacheSettings
{
get
{
return (SyncCacheConfiguration)base["syncCacheSettings"];
}
set
{
this["syncCacheSettings"] = value;
}
}
}

配置文件的写法在summary里
tete 2008-11-17
  • 打赏
  • 举报
回复
Configuration.GetSection
试试
不行就用读取xml的方法。

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧