using System; using System.Configuration; using System.Collections.Generic; namespace SuperSocket.Common { /// /// GenericConfigurationElementCollectionBase /// /// The type of the config element. /// The type of the config interface. public class GenericConfigurationElementCollectionBase : ConfigurationElementCollection, IEnumerable where TConfigElement : ConfigurationElement, TConfigInterface, new() { /// /// Gets or sets a property, attribute, or child element of this configuration element. /// /// The specified property, attribute, or child element public TConfigElement this[int index] { get { return (TConfigElement)base.BaseGet(index); } set { if (base.BaseGet(index) != null) { base.BaseRemoveAt(index); } this.BaseAdd(index, value as ConfigurationElement); } } /// /// When overridden in a derived class, creates a new . /// /// /// A new . /// protected override ConfigurationElement CreateNewElement() { return new TConfigElement() as ConfigurationElement; } /// /// Gets the element key for a specified configuration element when overridden in a derived class. /// /// The to return the key for. /// /// An that acts as the key for the specified . /// protected override object GetElementKey(ConfigurationElement element) { return element; } #region IEnumerable[T] implementation /// /// Returns an enumerator that iterates through the collection. /// /// /// A that can be used to iterate through the collection. /// public new IEnumerator GetEnumerator() { int count = base.Count; for (int i = 0; i < count; i++) { yield return (TConfigElement)base.BaseGet(i); } } #endregion } /// /// GenericConfigurationElementCollection /// /// The type of the config element. /// The type of the config interface. public class GenericConfigurationElementCollection : GenericConfigurationElementCollectionBase, IEnumerable where TConfigElement : ConfigurationElementBase, TConfigInterface, new() { /// /// Gets the element key. /// /// The element. /// protected override object GetElementKey(ConfigurationElement element) { return ((TConfigElement)element).Name; } } }