0

We are looking at following this sample which was very helpful:- Stack overflow answer

however, can someone please assist with how to actually access the custom config item based on the name?

I realize that it is possible to do a for each and then get the correct one, however, we would really like to access it like :-

config.Instances["Tata Motors"]
Community
  • 1
  • 1
Simon
  • 1,412
  • 4
  • 20
  • 48

1 Answers1

2

Since the indexer of ConfigurationElementCollection is internal, you'll have to introduce your own indexer in MyConfigInstanceCollection:

public class MyConfigInstanceCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new MyConfigInstanceElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        //set to whatever Element Property you want to use for a key
        return ((MyConfigInstanceElement)element).Name;
    }

    public new MyConfigInstanceElement this[string key]
    {
        get { return BaseGet(key) as MyConfigInstanceElement; }
    }
}
haim770
  • 48,394
  • 7
  • 105
  • 133
  • brilliant, do you recommend any further reading material on this subject? – Simon May 27 '14 at 13:50
  • Thanks. I'm no expert in custom configuration but http://www.codeproject.com/Articles/16724/Decoding-the-Mysteries-of-NET-Configuration seems rather comprehensive. – haim770 May 27 '14 at 14:41