Getting config element from ConfigurationElementCollection
Asked Answered
D

2

7

I have (hopefully) setup ConfigurationElementCollection of my own design with emails as keys. Now what? Hard to find actually on the web. How do I:

  1. iterate through it?

  2. See if a specific element exists?

  3. get a specific element?

...given:

    YourConfigElement config = 
   ConfigurationManager.GetSection("YourSectionName") as YourConfigElement;

Partial answer

1.

 foreach (X x in config.XCollection) 
             <code here>

2 . replace "code here" with

 { 
    if (x.Y == needle) 
    {
       hasIndeed = true;
       break;
    }
 }

3 . replace "code here" with

 { if (x.Y == needle) 
       cameUpWith = x;
       break;
 }

Tiny odor.

Dowitcher answered 6/5, 2010 at 19:47 Comment(0)
A
8

What you want is your own generic ConfigurationElementCollection base class which implements IList<T>. You can then inherit from this for all your configuration collections and cut down on the amount of work you need to do when creating configuration collections.

public abstract class BaseConfigurationElementCollection<TConfigurationElementType> : ConfigurationElementCollection, IList<TConfigurationElementType> where TConfigurationElementType : ConfigurationElement, IConfigurationElementCollectionElement, new()
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new TConfigurationElementType();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((TConfigurationElementType)element).ElementKey;
    }

    public IEnumerator<TConfigurationElement> GetEnumerator()
    {
        foreach (TConfigurationElement type in this)
        {
            yield return type;
        }
    }

    public void Add(TConfigurationElementType configurationElement)
    {
        BaseAdd(configurationElement, true);
    }

    public void Clear()
    {
        BaseClear();
    }

    public bool Contains(TConfigurationElementType configurationElement)
    {
        return !(IndexOf(configurationElement) < 0);
    }

    public void CopyTo(TConfigurationElementType[] array, int arrayIndex)
    {
        base.CopyTo(array, arrayIndex);
    }

    public bool Remove(TConfigurationElementType configurationElement)
    {
        BaseRemove(GetElementKey(configurationElement));

        return true;
    }

    bool ICollection<TConfigurationElementType>.IsReadOnly
    {
        get { return IsReadOnly(); }
    }

    public int IndexOf(TConfigurationElementType configurationElement)
    {
        return BaseIndexOf(configurationElement);
    }

    public void Insert(int index, TConfigurationElementType configurationElement)
    {
        BaseAdd(index, configurationElement);
    }

    public void RemoveAt(int index)
    {
        BaseRemoveAt(index);
    }

    public TConfigurationElementType this[int index]
    {
        get
        {
            return (TConfigurationElementType)BaseGet(index);
        }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
            BaseAdd(index, value);
        }
    }
}

With a little bit more work you can have a dictionary collection as well.

Andras answered 2/7, 2010 at 8:31 Comment(0)
L
2

I don't totally understand what your issues are - but basically, if you have a custom configuration element, you should be able to retrieve that from the config file using something like:

YourConfigElement config = 
    ConfigurationManager.GetSection("YourSectionName") as YourConfigElement ;

Once you have your configuration element, you can do with it whatever you like - you can implement all those things you asked for - check existance of an element, get a specific element etc.

You should also check out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject for more information - maybe those articles will help you unlock your config "challenge" ;-)

Highly recommended, well written and extremely helpful!

And if you haven't discovered it already - there's an excellent Configuration Section Designer up on Codeplex which makes visually designing configuration sections and collections a snap and writes all the gooey glue code for you - very handy indeed!

Marc

Luker answered 6/5, 2010 at 20:26 Comment(2)
Thanks for your reply. The hard part for me is collection types. I've solved #1 but it seems there should be a prettier and faster way if you're looking for one key. I'll check your links.Dowitcher
I'm actually quite amused. The pages you suggest are not the first of similar content I've visited. By now I'm proficient in defining collection types, adding elements in the config, all but actually using the collection in my code. It exposes the AsQueryable() method. I'm sure that's a clue, but the queryable in turn doesn't expose anything of use - get or contains, for instance.Dowitcher

© 2022 - 2024 — McMap. All rights reserved.