ProviderCollection.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #if NET_2_0
  30. using System.Collections;
  31. namespace System.Configuration.Provider {
  32. public class ProviderCollection : ICollection
  33. {
  34. public ProviderCollection ()
  35. {
  36. lookup = new Hashtable (10, CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);
  37. values = new ArrayList ();
  38. }
  39. public virtual void Add (ProviderBase provider)
  40. {
  41. if (readOnly)
  42. throw new NotSupportedException ();
  43. if (provider == null || provider.Name == null)
  44. throw new ArgumentNullException ();
  45. int pos = values.Add (provider);
  46. try {
  47. lookup.Add (provider.Name, pos);
  48. } catch {
  49. values.RemoveAt (pos);
  50. throw;
  51. }
  52. }
  53. public void Clear ()
  54. {
  55. if (readOnly)
  56. throw new NotSupportedException ();
  57. values.Clear ();
  58. lookup.Clear ();
  59. }
  60. public void CopyTo (ProviderBase[] array, int index)
  61. {
  62. values.CopyTo (array, index);
  63. }
  64. void ICollection.CopyTo (Array array, int index)
  65. {
  66. values.CopyTo (array, index);
  67. }
  68. public IEnumerator GetEnumerator ()
  69. {
  70. return values.GetEnumerator();
  71. }
  72. public void Remove (string name)
  73. {
  74. if (readOnly)
  75. throw new NotSupportedException ();
  76. object position = lookup [name];
  77. if (position == null || !(position is int))
  78. throw new ArgumentException ();
  79. int pos = (int) position;
  80. if (pos >= values.Count)
  81. throw new ArgumentException ();
  82. values.RemoveAt (pos);
  83. lookup.Remove (name);
  84. ArrayList changed = new ArrayList ();
  85. foreach (DictionaryEntry de in lookup) {
  86. if ((int) de.Value <= pos)
  87. continue;
  88. changed.Add (de.Key);
  89. }
  90. foreach (string key in changed)
  91. lookup [key] = (int)lookup [key] - 1;
  92. }
  93. public void SetReadOnly ()
  94. {
  95. readOnly = true;
  96. }
  97. public int Count { get { return values.Count; } }
  98. public bool IsSynchronized { get { return false; } }
  99. public object SyncRoot { get { return this; } }
  100. public ProviderBase this [string name] {
  101. get {
  102. object pos = lookup [name];
  103. if (pos == null) return null;
  104. return values [(int) pos] as ProviderBase;
  105. }
  106. }
  107. Hashtable lookup;
  108. bool readOnly;
  109. ArrayList values;
  110. }
  111. }
  112. #endif