2

I'm having trouble finding/figuring out how to map the following custom config section:

<section>
    <collection1>
        <subitem1 ... />
        <subitem1 ... />
    </collection1>
    <collection2>
        <subitem2 ... />
        <subitem2 ... />
    </collection2>
</section>

For a single sub collection, the following could would work:

public class Section : ConfigurationSection
{
    [ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
    [ConfigurationCollection(typeof(SubItem1Collection), AddItemName = "collection1")]
    public SubItem1Collection Collection1
    {
        get { return (SubItem1Collection)this[string.Empty]; }
        set { this[string.Empty] = value; }
    }
}

When I try to add the second collection, it wont run.

public class Section : ConfigurationSection
{
    [ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
    [ConfigurationCollection(typeof(TemplateCollection), AddItemName = "collection1")]
    public SubItem1Collection Collection1
    {
        get { return (SubItem1Collection)this[string.Empty]; }
        set { this[string.Empty] = value; }
    }

    [ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
    [ConfigurationCollection(typeof(SubItem2Collection), AddItemName = "collection2")]
    public SubItem2Collection Collection2
    {
        get { return (SubItem2Collection)this[string.Empty]; }
        set { this[string.Empty] = value; }
    }
}

The error is:

Unable to cast object of type 'SubItem1Collection' to type 'SubItem2Collection'.

The error is obviously in the indexer this[string.Empty];. Can anybody point me in the right direction on this?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Paul Fleming
  • 24,238
  • 8
  • 76
  • 113
  • Your first one has `ConfigurationCollection(typeof(TemplateCollection), ...` where your second has `ConfigurationCollection(typeof(SubItem2Collection), ...` – Thymine Nov 15 '12 at 23:17
  • @Thymine. Typo. Obviously my collections are not actually called "SubItem2Collection". I simply renamed them to remove any confusion regarding what I'm trying to achieve. – Paul Fleming Nov 16 '12 at 09:36
  • Ah I was wondering if they were both supposed to be `TemplateCollection`, not change between the two. Was thinking a typo like that might have caused that casting error in your original code as well. I've only needed 1 non-default collection in a config before so wasn't certain on any better solution so thought I'd point that out :) – Thymine Nov 16 '12 at 16:31
  • @Thymine Thanks for pointing it out. I was in the same boat, only really needed a single collection. Answer from fsimonazzi was exactly the problem. I'm surprised there are so few real-world examples on the net. – Paul Fleming Nov 16 '12 at 18:17

1 Answers1

3

You cannot have two default collections. To have configuration classes matching your xml sample, you will have to assign the names "collection1" and "collection2" respectively instead of just "" and set IsDefaultCollection to false in the ConfigurationProperty attributes, and set the AddItemName to "subitem1" and "subitem2" for the ConfigurationCollection attributes. Also fix what @thymine pointed out about the type of the second collection.

fsimonazzi
  • 2,975
  • 15
  • 13