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