How can I get and use an attribute set in the parent ConfigurationSection in the descendent CustomSetting element? I need this attribute when the CustomSetting element is returning the Value property.
I want to format the App.config like this:
<CustomSettings someProperty="foo">
<CustomSetting key="bar" value="fermeneba" />
<CustomSetting key="laa" value="jubaduba" />
</CustomSettings>
I have the code working, except that I cannot find a way to access the someProperty attribute from the CustomSetting class. The only way that I've found, so far, is to format the configuration like this, which is messy:
<CustomSettings>
<CustomSetting someProperty="foo" key="bar" value="fermeneba" />
<CustomSetting someProperty="foo" key="laa" value="jubaduba" />
</CustomSettings>
Achieving this is more difficult than it should be since the System.Configuration API doesn't allow you to navigate from a
ConfigurationElementto its parent. Hence, if you want to access some information that on a parent element you need to create that relationship manually. I've put together a sample implementation that does that for the config snippet in your question:You can see that the
CustomSettingElementCollectionhas aSectionproperty which gets set in the section'sElementsgetter. TheCustomSettingElement, in turn, has aParentproperty which gets set in the collection'sCreateNewElement()method.That then makes it possible to walk up the relationship tree and to add a
SomePropertyproperty to the element even though this one doesn't correspond to an actual ConfigurationProperty on that element.Hope that gives you an idea how to solve your problem!