1

When you inherit ConfigurationElementCollection:

public class Directories : ConfigurationElementCollection
{
    ...
}

ConfigurationElementCollection requires an implementation for GetElementKey(System.Configuration.ConfigurationElement).

But I don't care about keys since I have a custom configuration section like this:

<directorySection>
  <directories>
    <directory pickUpDirectory="C:\Users\User\Documents\PickUp\0" dropOffDirectory="C:\Users\User\Documents\DropOff\0"/>
    <directory pickUpDirectory="C:\Users\User\Documents\PickUp\0" dropOffDirectory="C:\Users\User\Documents\DropOff\0"/>
    <directory pickUpDirectory="C:\Users\User\Documents\PickUp\1" dropOffDirectory="C:\Users\User\Documents\DropOff\1"/>
    <directory pickUpDirectory="C:\Users\User\Documents\PickUp\2" dropOffDirectory="C:\Users\User\Documents\DropOff\2"/>
  </directories>
</directorySection>

It can have multiple elements, and each element is key-less, and this structure should allow for duplicates (as above). So what should I do in this case?

Alexandru
  • 12,264
  • 17
  • 113
  • 208
  • 1
    Then don't inherit `ConfigurationElementCollection` :) Use another way to parse/generate your xml. – L.B Oct 28 '14 at 19:21
  • @L.B I was going to go down that road initially, but now its like I have no other choice; nothing supports this out of the box with custom .config sections? – Alexandru Oct 28 '14 at 19:22
  • Have you tried just reading your application config file for your custom section and deserializing it into whatever class suits your needs? – Louis Oct 28 '14 at 19:27
  • @Louis I think L.B. suggested this – Alexandru Oct 28 '14 at 19:30
  • 1
    Maybe I misunderstood @L.B answer, I felt it was more generic. Now that I read mine, its actually not so different. Anyway, you dont need to implement ConfigurationElement at all as long as you know the XML structure. You could always read ApplicationSettings["directories"] using Linq XML for example. Maybe I fail to see your requirements? – Louis Oct 28 '14 at 19:33
  • @Louis That's a good idea; I'll give that a shot – Alexandru Oct 28 '14 at 19:35
  • @Louis It actually gets so much more complicated...maybe if you provide a link so that I can better understand how you would do it this way? But...the problem I am facing now is this: when you define a custom configuration section, the `
    ` tag requires both `name` and `type` attributes, and since I am loading all of this from a Windows service, if the `type` name is not available in my assembly and/or it does not exactly match the structure of the XAML in my .config file, it will crash my service on startup with a `System.TypeLoadException`.
    – Alexandru Oct 28 '14 at 20:18
  • Put it this way, guys, in the end, does this mean I have to parse the entire .config file myself? Because all roads lead to a dead end except parsing the entire .config myself and essentially making my own `ConfigurationManager` implementation. – Alexandru Oct 28 '14 at 20:22
  • I'm going to stop jumping through ridiculous hoops now. I went with using `var document = XDocument.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);`, and then parsing that for what I need using LINQ queries and such. – Alexandru Oct 28 '14 at 20:40

2 Answers2

4

Try to use ObjectIDGenerator like this

[ConfigurationCollection(typeof(SchedulerTaskItem), AddItemName = "Task")]
public class SchedulerTaskCollection: ConfigurationElementCollection
{
    private static readonly ObjectIDGenerator idGenerator = new ObjectIDGenerator();

    protected override ConfigurationElement CreateNewElement()
    {
        return new SchedulerTaskItem();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        bool firstTime;
        return idGenerator.GetId(element, out firstTime);
    }
}
0

This is a very old question, but I just ran into this issue.

Here's the general data structure I'm working with, though I'll probably tweak the actual implementation as it goes:

<filter>
    <group type="and|or|not">
        <condition field="..." operator="..." value="...">
        <group type="...">
            <condition field="..." operator="..." value="...">
        </group>
    </group>
</filter>

I came up with this as the solution.

protected override System.Object GetElementKey(System.Configuration.ConfigurationElement element)
{
    return (this.BaseIndexOf(element));
}
Theo Brinkman
  • 291
  • 2
  • 10