具有基本类型的ConfigurationElementCollection
本文关键字:ConfigurationElementCollection 类型 | 更新日期: 2023-09-27 18:02:00
我正在与系统合作。用于存储应用程序配置的配置名称空间类型。我需要存储一个基本类型的集合(System.Double)作为该配置的一部分。创建以下代码似乎有点小题大做:
[ConfigurationCollection(typeof(double), AddItemName="TemperaturePoint",
CollectionType=ConfigurationElementCollectionType.BasicMap)]
class DoubleCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return // Do I need to create a custom ConfigurationElement that wraps a double?
}
protected override object GetElementKey(ConfigurationElement element)
{
return // Also not sure what to do here
}
}
我无法想象我是第一个遇到这个问题的人。什么好主意吗?
没有明确的"嘿,我想在这里填充一个值列表"处理程序,但你有几个选项:
实现自定义IConfigurationSectionHandler
(比元素集合等简单得多)并通过:
<configSections>
<sectionGroup name="mysection" type="type of handler"/>
</configSections>
<mysection>
some xml representation of values
</mysection>
使用一个现有的处理程序,比如SingleTagSectionHandler
——这里有一个看起来很复杂的一行,它从配置文件中的这个条目中提取了一组值:
<configuration>
<configSections>
<section name="TemperaturePoints"
type="System.Configuration.SingleTagSectionHandler"
allowLocation="true"
allowDefinition="Everywhere"/>
</configSections>
<TemperaturePoints values="1,2,3,4,5,6,7,8,9,10"/>
</configuration>
var values = ((string)((Hashtable)ConfigurationManager
.GetSection("TemperaturePoints"))["values"])
.Split(',')
.Select(double.Parse);
或者分开一点:
var section = (Hashtable)ConfigurationManager.GetSection("TemperaturePoints");
var packedValues = (string)section["values"];
var unpackedValues = packedValues.Split(',');
var asDoubles = unpackedValues.Select(double.Parse).ToArray();
我能够在没有太多定制的情况下使其工作。它类似于JerKimball的回答,但我避免处理自定义字符串处理,通过使用一个TypeConverter属性的ConfigurationProperty。
我的自定义配置节实现:
using System.Configuration;
using System.ComponentModel;
class DomainConfig : ConfigurationSection
{
[ConfigurationProperty("DoubleArray")]
[TypeConverter(typeof(CommaDelimitedStringCollectionConverter))]
public CommaDelimitedStringCollection DoubleArray
{
get { return (CommaDelimitedStringCollection)base["DoubleArray"]; }
}
}
用法:
var doubleValues = from string item in configSection.DoubleArray select double.Parse(item);
和配置文件:
<DomainConfig DoubleArray="1.0,2.0,3.0"></DomainConfig>
这是我感觉正确的实现。
- 每个值在单独的行上(为了方便差异)
- 高信噪比编码的最小开销
- 简单读取值
底部提供了有限的解释。如果你想了解更多的系统基础知识,我推荐Jon Rista在CodeProject.com上发表的《揭开。net 2.0配置的奥秘》系列文章。配置API。
应用程序。配置
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="strings"
type="Sample.StringCollectionConfigSection, SampleAssembly"/>
<section name="databases"
type="Sample.StringCollectionConfigSection, SampleAssembly"/>
</configSections>
<strings>
<add>dbo.Foo</add>
<add>dbo.Bar</add>
</strings>
<databases>
<add>Development</add>
<add>Test</add>
<add>Staging</add>
</databases>
</configuration>
API使用
class Program
{
static void Main(string[] args)
{
foreach (var s in StringCollectionConfigSection.Named("strings"))
{
Console.WriteLine(ignoreExpression);
}
foreach (var d in StringCollectionConfigSection.Named("strings"))
{
Console.WriteLine(ignoreExpression);
}
}
}
实施using System;
using System.Collections.Generic;
using System.Configuration;
using System.Xml;
hnamespace Sample
{
public sealed class StringCollectionConfigSection : ConfigurationSection
{
public static StringElementCollection Named(string configSection)
{
var section = (StringCollectionConfigSection)ConfigurationManager.GetSection(configSection);
return section.Elements;
}
[ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
public StringElementCollection Elements
{
get { return (StringElementCollection)base[""]; }
set { base[""] = value; }
}
}
[ConfigurationCollection(typeof(StringElement))]
public sealed class StringElementCollection : ConfigurationElementCollection, IEnumerable<string>
{
public StringElement this[int index]
{
get { return (StringElement)BaseGet(index); }
set
{
if (BaseGet(index) != null) { BaseRemoveAt(index); }
BaseAdd(index, value);
}
}
public new StringElement this[string key]
{
get { return (StringElement)BaseGet(key); }
}
protected override ConfigurationElement CreateNewElement()
{
return new StringElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((StringElement)element).Value;
}
public new IEnumerator<string> GetEnumerator()
{
var enumerator = base.GetEnumerator();
while (enumerator.MoveNext())
{
yield return ((StringElement)enumerator.Current).Value;
}
}
}
public class StringElement : ConfigurationElement
{
protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
{
Value = (string)reader.ReadElementContentAs(typeof(string), null);
}
public string Value {get; private set; }
}
}
学习代码的要点。
- 在app.config中,确保在定义配置部分的名称时使用命名空间和程序集名称。
- 我不想在集合中添加额外的子元素。我只需要保存字符串值的元素。
- 在
StringCollectionConfigSection
类的StringElementCollection Elements
属性上定义的ConfigurationPropertyAttribute
使用空名称来完成我想要实现的默认收集样式。
- 在
<strings>
<elements>
<add>...</add>
<add>...</add>
<elements>
</strings>
在
StringElement
上的DeserializeElement
允许我使用XmlNode的innerText作为值,而不是一个属性。ConfigurationElementCollection
上的IEnumerator<string>
加上StringCollectionConfigSection
上的StringElementCollection Named(string configSection)
给了我想要的干净的API。