Monday, January 23, 2012

Strongly Typed Configuration Sections

I generally don't like to use strongly typed configuration sections, but I'm working on a project that uses them very heavily and just encountered an annoying issue.

When you have a configuration collection such as shown below, there is no way to access the value of customAttribute in the your strongly typed collection class.

<folderlist customattribute="value">
<clear>
<add attribute1="value" attribute2="value">
<add attribute1="value" attribute2="value">
<folderlist>

The link below shows an example of how this can be resolved, but it is a bit nasty.

http://www.frankwisniewski.net/2011/12/how-to-use-a-configurationelementcollection-with-custom-attributes/

One thing to note is that in your class definition, you cannot use the [ConfigurationProperty] attribute on the property exposing the attribute, otherwise the property will not be identified as an unrecognised attribute and so your code will never be hit..

No comments:

Post a Comment