Organizational Research By

Surprising Reserch Topic

Question:ConfigurationElementCollection with primitive types


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?

asked Sep 13, 2013 in ANDROID by rajesh
edited Sep 12, 2013
0 votes
56 views



Related Hot Questions

2 Answers

0 votes
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:


    



  some xml representation of values


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:


    
        
             type="System.Configuration.SingleTagSectionHandler"
             allowLocation="true"
             allowDefinition="Everywhere"/>
    

    


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();
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes
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:


    



  some xml representation of values


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:


    
        
             type="System.Configuration.SingleTagSectionHandler"
             allowLocation="true"
             allowDefinition="Everywhere"/>
    

    


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();
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013

...