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
ConfigurationElement
to 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
CustomSettingElementCollection
has aSection
property which gets set in the section'sElements
getter. TheCustomSettingElement
, in turn, has aParent
property which gets set in the collection'sCreateNewElement()
method.That then makes it possible to walk up the relationship tree and to add a
SomeProperty
property 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!