ProviderCollection.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // System.Configuration.Provider.ProviderCollection
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2003 Ben Maurer
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Collections;
  30. namespace System.Configuration.Provider {
  31. public class ProviderCollection : ICollection
  32. {
  33. public ProviderCollection ()
  34. {
  35. lookup = new Hashtable (10, StringComparer.InvariantCultureIgnoreCase);
  36. values = new ArrayList ();
  37. }
  38. public virtual void Add (ProviderBase provider)
  39. {
  40. if (readOnly)
  41. throw new NotSupportedException ();
  42. if (provider == null || provider.Name == null)
  43. throw new ArgumentNullException ();
  44. int pos = values.Add (provider);
  45. try {
  46. lookup.Add (provider.Name, pos);
  47. } catch {
  48. values.RemoveAt (pos);
  49. throw;
  50. }
  51. }
  52. public void Clear ()
  53. {
  54. if (readOnly)
  55. throw new NotSupportedException ();
  56. values.Clear ();
  57. lookup.Clear ();
  58. }
  59. public void CopyTo (ProviderBase[] array, int index)
  60. {
  61. values.CopyTo (array, index);
  62. }
  63. void ICollection.CopyTo (Array array, int index)
  64. {
  65. values.CopyTo (array, index);
  66. }
  67. public IEnumerator GetEnumerator ()
  68. {
  69. return values.GetEnumerator();
  70. }
  71. public void Remove (string name)
  72. {
  73. if (readOnly)
  74. throw new NotSupportedException ();
  75. object position = lookup [name];
  76. if (position == null || !(position is int))
  77. throw new ArgumentException ();
  78. int pos = (int) position;
  79. if (pos >= values.Count)
  80. throw new ArgumentException ();
  81. values.RemoveAt (pos);
  82. lookup.Remove (name);
  83. ArrayList changed = new ArrayList ();
  84. foreach (DictionaryEntry de in lookup) {
  85. if ((int) de.Value <= pos)
  86. continue;
  87. changed.Add (de.Key);
  88. }
  89. foreach (string key in changed)
  90. lookup [key] = (int)lookup [key] - 1;
  91. }
  92. public void SetReadOnly ()
  93. {
  94. readOnly = true;
  95. }
  96. public int Count { get { return values.Count; } }
  97. public bool IsSynchronized { get { return false; } }
  98. public object SyncRoot { get { return this; } }
  99. public ProviderBase this [string name] {
  100. get {
  101. object pos = lookup [name];
  102. if (pos == null) return null;
  103. return values [(int) pos] as ProviderBase;
  104. }
  105. }
  106. Hashtable lookup;
  107. bool readOnly;
  108. ArrayList values;
  109. }
  110. }