Vishful thinking…

Using custom names for the ConfigurationElementCollection entries

Posted in Uncategorized by viswaug on September 19, 2007

I have been using the .NET configuration library pretty extensively and one thing about using the ConfigurationElementCollection kind of just kept bothering me. All of my custom sections implementing ConfigurationElementCollection always used “add” as the default element name for its entries. I wanted it to be named something more meaningful than “add” which did not make sense all the time. For example, I wanted to be able to use “entry” as the element name instead of “add” like shown below.

ConfigurationElementCollection

Nothing the documentation for the .NET configuration stood out to me that will let me use a more meaningful name. Using a different custom name for the element entries didn’t turn out to be hard to. It was just a matter of looking in the right places to figure how to get it done. This can be accomplished by decorating the property of the custom ConfigurationElementCollection type in the ConfigurationSection with the ConfigurationCollectionAttribute and specifying the “AddItemName” which is an optional parameter like shown below.

    public class TestSection: ConfigurationSection

    {

        public TestSection() { }

 

        [ConfigurationProperty(“NameValues”, IsDefaultCollection = true)]

        [ConfigurationCollection(typeof(NameValueElementCollection), AddItemName=“entry”)]

        public NameValueElementCollection NameValues

        {

            get { return (NameValueElementCollection)base[“NameValues”]; }

        }

    }

8 Responses

Subscribe to comments with RSS.

  1. […] Using custom names for the ConfigurationElementCollection entries […]

  2. Parag said, on July 16, 2009 at 12:50 am

    After spending 20 hrs trying (almost banging my head) to get a collection of custom configuration, I came across your article and in next 5 mins I got it working.

    You can not belive how thankful I am to you for posting this article!

  3. Collin said, on September 16, 2009 at 6:11 pm

    Thanks for the post. It helped me also. 🙂

  4. Håkan Fröling said, on September 24, 2009 at 7:47 pm

    Great! Thanx for the tip, it helped.

  5. Christian Palm said, on October 22, 2009 at 10:43 am

    Thanks, works like a charm

  6. Harald Mühlhoff said, on January 21, 2010 at 6:14 pm

    Definitely saved me some time! Thanks!

  7. Jon said, on July 21, 2010 at 12:46 pm

    Many thanks, this was driving me insane

  8. Aaron said, on September 7, 2011 at 11:11 am

    hi instead of namevaluecollection i want to use my custom class(X) collection which has 4 properties. How do i do that ?

    It says Class X as “Unrecognized element”.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: