-
-
Save Antaris/1664214 to your computer and use it in GitHub Desktop.
<configuration> | |
<configSections> | |
<section name="test" type="ConsoleApplication1.FooBarConfigurationSection, ConsoleApplication1" /> | |
</configSections> | |
<test> | |
<items> | |
<bar name="barOne" barValue="Bartastic" /> | |
<foo name="fooOne" fooValue="Footastic" /> | |
<bar name="barTwo" barValue="SoMuchBarIsUnbelievable" /> | |
</items> | |
</test> | |
</configuration> |
public class BarConfigurationElement : NamedConfigurationElementBase | |
{ | |
private const string BarValueAttribute = "barValue"; | |
[ConfigurationProperty(BarValueAttribute, IsRequired = true)] | |
public string BarValue | |
{ | |
get { return (string)this[BarValueAttribute]; } | |
} | |
} |
[ConfigurationCollection(typeof(NamedConfigurationElementBase))] | |
public class FooBarConfigurationElementCollection : ConfigurationElementCollection | |
{ | |
protected override object GetElementKey(ConfigurationElement element) | |
{ | |
return ((NamedConfigurationElementBase)element).Name; | |
} | |
protected override ConfigurationElement CreateNewElement() | |
{ | |
return null; | |
} | |
protected override ConfigurationElement CreateNewElement(string elementName) | |
{ | |
switch (elementName) | |
{ | |
case "foo": | |
return new FooConfigurationElement(); | |
case "bar": | |
return new BarConfigurationElement(); | |
} | |
throw new ConfigurationErrorsException("Unsupported element: " + elementName); | |
} | |
protected override bool OnDeserializeUnrecognizedElement(string elementName, XmlReader reader) | |
{ | |
if (elementName.Equals("one") || elementName.Equals("two")) | |
{ | |
var element = (NamedConfigurationElementBase)CreateNewElement(elementName); | |
element.Deserialize(reader); | |
BaseAdd(element); | |
return true; | |
} | |
return base.OnDeserializeUnrecognizedElement(elementName, reader); | |
} | |
} |
public class FooBarConfigurationSection : ConfigurationSection | |
{ | |
private const string ItemsElement = "items"; | |
[ConfigurationCollection(ItemsElement)] | |
public FooBarConfigurationElementCollection Items | |
{ | |
get { return (FooBarConfigurationElementCollection)this[ItemsElement]; } | |
} | |
} |
public class FooConfigurationElement : NamedConfigurationElementBase | |
{ | |
private const string FooValueAttribute = "fooValue"; | |
[ConfigurationProperty(FooValueAttribute, IsRequired = true)] | |
public string FooValue | |
{ | |
get { return (string)this[FooValueAttribute]; } | |
} | |
} |
public abstract class NamedConfigurationElementBase | |
{ | |
private const string NameAttribute = "name"; | |
[ConfigurationProperty(NameAttribute, IsRequired = true)] | |
public string Name | |
{ | |
get { return (string)this[NameAttribute]; } | |
} | |
internal void Deserialize(XmlReader reader) | |
{ | |
base.DeserializeElement(reader, false); | |
} | |
} |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var config = ConfigurationManager.GetSection("test") as FooBarConfigurationSection; | |
foreach (var namedConfig in config.Items.Cast<NamedConfigurationSectionBase>()) | |
{ | |
if (namedConfig is FooConfigurationSection) | |
Console.WriteLine(((FooConfigurationSection)).FooValue); | |
else if (namedConfig is BarConfigurationSection) | |
Console.WriteLine(((BarConfigurationSection)).BarValue); | |
} | |
} | |
} |
I came across this same code on snippetrepo.com. I followed the example and it was exactly what i needed. Works great when reading a config file. However, when I went to save the configuration I ran into issues. It appears that when saving .NET framework seems to be calling GetElementKey which returns the value of the element's key. Which then is passed to CreateNewElement(string elementName). Obviously this is not the intended purpose of the design. CreateNewElement excpects the element's ElementType.Name in order to return a new instance of the specific sub-type. I don't know exactly how the process of saving a configuration object to the app.config file works but it seems like there are different expectations with regard to these 2 methods when saving config as opposed to reading. Is there something I'm missing? Is there a way to solve this problem?
To create configuration collections that handle multiple element types, we have to specialise the configuration collection and override the
OnDeserializeUnrecognizedElement
to intercept the creation of our elements. Because we are doing this outside ofSystem.Configuration.dll
, we can't then manually invoke theDeserializeElement
method on the new element, so by providing aDeserialize
method on our base element type (NamedConfigurationElemementBase
), we can invoke the protectedDeserializeElement
method to complete the element construction.The net effect is, we can now create configuration collections that contain multiple element types.