I'm working with the System.Configuration namespace types to store configuration for my application. I need to store a collection of primitive types (System.Double) as part of that configuration. It seems like overkill to create the following:
[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
}
}
I can't imagine I'm the first person to encounter this problem. Any ideas?
I was able to get this to work without much customization. It is similar to JerKimball's answer but I avoids processing the custom string processing by using a TypeConverter attribute for the ConfigurationProperty.
My custom config section implementation:
using System.Configuration;
using System.ComponentModel;
class DomainConfig : ConfigurationSection
{
[ConfigurationProperty("DoubleArray")]
[TypeConverter(typeof(CommaDelimitedStringCollectionConverter))]
public CommaDelimitedStringCollection DoubleArray
{
get { return (CommaDelimitedStringCollection)base["DoubleArray"]; }
}
}
How it's used:
var doubleValues = from string item in configSection.DoubleArray select double.Parse(item);
And the config file:
<DomainConfig DoubleArray="1.0,2.0,3.0"></DomainConfig>