Question

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?

Was it helpful?

Solution 2

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>

OTHER TIPS

There's no explicit "hey, I want to stuff a list of values in here" handler, but you have a few options:

Implement a custom IConfigurationSectionHandler (way simpler than the element collection, etc) and reference via:

<configSections>
    <sectionGroup name="mysection" type="type of handler"/>
</configSections>

<mysection>
  some xml representation of values
</mysection>

Piggyback on one of the existing handlers, like SingleTagSectionHandler - here's a hairy-looking one liner that extracts a set of values from this entry in config file:

<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);

Or split up a bit:

var section = (Hashtable)ConfigurationManager.GetSection("TemperaturePoints");
var packedValues = (string)section["values"];
var unpackedValues = packedValues.Split(',');
var asDoubles = unpackedValues.Select(double.Parse).ToArray();

This is the implementation that feels right to me.

  • Each value on a separate line (for easy diffs)
  • Minimal overhead for the encoding with high signal to noise
  • Simple reading of the values

Limited explanation provided at bottom. I recommend Jon Rista's Unraveling the Mysteries of .NET 2.0 Configuration article series on CodeProject.com if you want to know more of the basics of the System.Configuration API.

APP.CONFIG

<?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 Usage

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);
        }
    }
}

IMPLEMENTATION

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; }
    }
}

Learning points about the code.

  • In the app.config, make sure you use the namespace and assembly name when defining the name of your config section.
  • I did not want the extra sub-element in my collection. I only wanted the elements that held the string values.
    • The ConfigurationPropertyAttribute defined on the StringElementCollection Elements property in the StringCollectionConfigSection class uses an empty name to accomplish the default collection style I wanted to achieve.

<strings>
    <elements>
       <add>...</add>
       <add>...</add>
    <elements>
</strings>
  • The DeserializeElement on StringElement allows me to use the innerText of the XmlNode as the value, instead of an Attribute.

  • The IEnumerator<string> on ConfigurationElementCollection coupled with StringElementCollection Named(string configSection) on StringCollectionConfigSection gives me the clean API I wanted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top