GenericConfigurationElementCollection.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Configuration;
  3. using System.Collections.Generic;
  4. namespace SuperSocket.Common
  5. {
  6. /// <summary>
  7. /// GenericConfigurationElementCollectionBase
  8. /// </summary>
  9. /// <typeparam name="TConfigElement">The type of the config element.</typeparam>
  10. /// <typeparam name="TConfigInterface">The type of the config interface.</typeparam>
  11. public class GenericConfigurationElementCollectionBase<TConfigElement, TConfigInterface> : ConfigurationElementCollection, IEnumerable<TConfigInterface>
  12. where TConfigElement : ConfigurationElement, TConfigInterface, new()
  13. {
  14. /// <summary>
  15. /// Gets or sets a property, attribute, or child element of this configuration element.
  16. /// </summary>
  17. /// <returns>The specified property, attribute, or child element</returns>
  18. public TConfigElement this[int index]
  19. {
  20. get
  21. {
  22. return (TConfigElement)base.BaseGet(index);
  23. }
  24. set
  25. {
  26. if (base.BaseGet(index) != null)
  27. {
  28. base.BaseRemoveAt(index);
  29. }
  30. this.BaseAdd(index, value as ConfigurationElement);
  31. }
  32. }
  33. /// <summary>
  34. /// When overridden in a derived class, creates a new <see cref="T:System.Configuration.ConfigurationElement"/>.
  35. /// </summary>
  36. /// <returns>
  37. /// A new <see cref="T:System.Configuration.ConfigurationElement"/>.
  38. /// </returns>
  39. protected override ConfigurationElement CreateNewElement()
  40. {
  41. return new TConfigElement() as ConfigurationElement;
  42. }
  43. /// <summary>
  44. /// Gets the element key for a specified configuration element when overridden in a derived class.
  45. /// </summary>
  46. /// <param name="element">The <see cref="T:System.Configuration.ConfigurationElement"/> to return the key for.</param>
  47. /// <returns>
  48. /// An <see cref="T:System.Object"/> that acts as the key for the specified <see cref="T:System.Configuration.ConfigurationElement"/>.
  49. /// </returns>
  50. protected override object GetElementKey(ConfigurationElement element)
  51. {
  52. return element;
  53. }
  54. #region IEnumerable[T] implementation
  55. /// <summary>
  56. /// Returns an enumerator that iterates through the collection.
  57. /// </summary>
  58. /// <returns>
  59. /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
  60. /// </returns>
  61. public new IEnumerator<TConfigInterface> GetEnumerator()
  62. {
  63. int count = base.Count;
  64. for (int i = 0; i < count; i++)
  65. {
  66. yield return (TConfigElement)base.BaseGet(i);
  67. }
  68. }
  69. #endregion
  70. }
  71. /// <summary>
  72. /// GenericConfigurationElementCollection
  73. /// </summary>
  74. /// <typeparam name="TConfigElement">The type of the config element.</typeparam>
  75. /// <typeparam name="TConfigInterface">The type of the config interface.</typeparam>
  76. public class GenericConfigurationElementCollection<TConfigElement, TConfigInterface> : GenericConfigurationElementCollectionBase<TConfigElement, TConfigInterface>, IEnumerable<TConfigInterface>
  77. where TConfigElement : ConfigurationElementBase, TConfigInterface, new()
  78. {
  79. /// <summary>
  80. /// Gets the element key.
  81. /// </summary>
  82. /// <param name="element">The element.</param>
  83. /// <returns></returns>
  84. protected override object GetElementKey(ConfigurationElement element)
  85. {
  86. return ((TConfigElement)element).Name;
  87. }
  88. }
  89. }