2 // System.Collections.DictionaryBase.cs
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System
.Runtime
.InteropServices
;
36 namespace System
.Collections
{
40 public abstract class DictionaryBase
: IDictionary
, ICollection
, IEnumerable
{
44 protected DictionaryBase ()
46 hashtable
= new Hashtable ();
58 return hashtable
.Count
;
62 protected IDictionary Dictionary
{
68 protected Hashtable InnerHashtable
{
74 public void CopyTo (Array array
, int index
)
77 throw new ArgumentNullException ("array");
79 throw new ArgumentOutOfRangeException ("index must be possitive");
81 throw new ArgumentException ("array is multidimensional");
82 int size
= array
.Length
;
84 throw new ArgumentException ("index is larger than array size");
85 if (index
+ Count
> size
)
86 throw new ArgumentException ("Copy will overlflow array");
88 DoCopy (array
, index
);
91 private void DoCopy (Array array
, int index
)
93 foreach (DictionaryEntry de
in hashtable
)
94 array
.SetValue (de
, index
++);
97 public IDictionaryEnumerator
GetEnumerator ()
99 return hashtable
.GetEnumerator ();
102 protected virtual void OnClear ()
106 protected virtual void OnClearComplete ()
110 protected virtual object OnGet (object key
, object currentValue
)
115 protected virtual void OnInsert (object key
, object value)
119 protected virtual void OnInsertComplete (object key
, object value)
123 protected virtual void OnSet (object key
, object oldValue
, object newValue
)
127 protected virtual void OnSetComplete (object key
, object oldValue
, object newValue
)
131 protected virtual void OnRemove (object key
, object value)
135 protected virtual void OnRemoveComplete (object key
, object value)
139 protected virtual void OnValidate (object key
, object value)
143 bool IDictionary
.IsFixedSize
{
149 bool IDictionary
.IsReadOnly
{
155 object IDictionary
.this [object key
] {
157 object value = hashtable
[key
];
162 OnValidate (key
, value);
163 object current_value
= hashtable
[key
];
164 OnSet (key
, current_value
, value);
165 hashtable
[key
] = value;
167 OnSetComplete (key
, current_value
, value);
169 hashtable
[key
] = current_value
;
175 ICollection IDictionary
.Keys
{
177 return hashtable
.Keys
;
181 ICollection IDictionary
.Values
{
183 return hashtable
.Values
;
187 void IDictionary
.Add (object key
, object value)
189 OnValidate (key
, value);
190 OnInsert (key
, value);
191 hashtable
.Add (key
, value);
193 OnInsertComplete (key
, value);
195 hashtable
.Remove (key
);
200 void IDictionary
.Remove (object key
)
202 if (!hashtable
.Contains (key
))
205 object value = hashtable
[key
];
206 OnValidate (key
, value);
207 OnRemove (key
, value);
208 hashtable
.Remove (key
);
210 OnRemoveComplete (key
, value);
212 hashtable
[key
] = value;
217 bool IDictionary
.Contains (object key
)
219 return hashtable
.Contains (key
);
222 bool ICollection
.IsSynchronized
{
224 return hashtable
.IsSynchronized
;
228 object ICollection
.SyncRoot
{
230 return hashtable
.SyncRoot
;
234 IEnumerator IEnumerable
.GetEnumerator ()
236 return hashtable
.GetEnumerator ();