Retrieving a configuration item from ConfigurationElementCollection

I (hopefully) have a ConfigurationElementCollection setting of my own design with keys letters. Now what? Hard to actually find on the Internet. Like me:

  • iterate through it?

  • See if there is any particular item?

  • get a specific item?

... Given:

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

Partial answer

one.

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

2. replace "code here" with

  { if (xY == needle) { hasIndeed = true; break; } } 

3. replace "code here" with

  { if (xY == needle) cameUpWith = x; break; } 

Little smell.

+7
c # system.configuration
source share
2 answers

What you want is your own common ConfigurationElementCollection base class that implements IList<T> . You can then inherit this for all of your configuration collections and reduce the amount of work required to create 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 work you can also have a collection of dictionaries.

+8
source share

I don’t quite understand what your problems are, but basically, if you have a custom configuration item, you should get it from the configuration file using something like:

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

As soon as you have your own configuration item, you can do whatever you want with it - you can implement all the things you asked for - check the existence of the element, get a specific element, etc.

You should also check out the Jon Rista three-part series on configuring .NET 2.0 on CodeProject for more information - perhaps these articles will help you unlock the "challenge" configuration; -)

Highly recommended, well written and very helpful!

And if you haven’t found it yet - there is an excellent Design Section Designer on Codeplex, which visually designs sections and configuration collections, clicking and writing down all the adhesive code is very convenient for you!

Mark

+2
source share

All Articles