Created
January 23, 2012 16:51
-
-
Save Antaris/1664214 to your computer and use it in GitHub Desktop.
Heterogeneous Configuration Collections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BarConfigurationElement : NamedConfigurationElementBase | |
{ | |
private const string BarValueAttribute = "barValue"; | |
[ConfigurationProperty(BarValueAttribute, IsRequired = true)] | |
public string BarValue | |
{ | |
get { return (string)this[BarValueAttribute]; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FooBarConfigurationSection : ConfigurationSection | |
{ | |
private const string ItemsElement = "items"; | |
[ConfigurationCollection(ItemsElement)] | |
public FooBarConfigurationElementCollection Items | |
{ | |
get { return (FooBarConfigurationElementCollection)this[ItemsElement]; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FooConfigurationElement : NamedConfigurationElementBase | |
{ | |
private const string FooValueAttribute = "fooValue"; | |
[ConfigurationProperty(FooValueAttribute, IsRequired = true)] | |
public string FooValue | |
{ | |
get { return (string)this[FooValueAttribute]; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?