DictionaryBase.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // System.Collections.DictionaryBase.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // (C) Ximian, Inc. http://www.ximian.com
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Runtime.InteropServices;
  33. namespace System.Collections {
  34. #if NET_2_0
  35. [ComVisible(true)]
  36. #endif
  37. [Serializable]
  38. public abstract class DictionaryBase : IDictionary, ICollection, IEnumerable {
  39. Hashtable hashtable;
  40. protected DictionaryBase ()
  41. {
  42. hashtable = new Hashtable ();
  43. }
  44. public void Clear ()
  45. {
  46. OnClear ();
  47. hashtable.Clear ();
  48. OnClearComplete ();
  49. }
  50. public int Count {
  51. get {
  52. return hashtable.Count;
  53. }
  54. }
  55. protected IDictionary Dictionary {
  56. get {
  57. return this;
  58. }
  59. }
  60. protected Hashtable InnerHashtable {
  61. get {
  62. return hashtable;
  63. }
  64. }
  65. public void CopyTo (Array array, int index)
  66. {
  67. if (array == null)
  68. throw new ArgumentNullException ("array");
  69. if (index < 0)
  70. throw new ArgumentOutOfRangeException ("index must be possitive");
  71. if (array.Rank > 1)
  72. throw new ArgumentException ("array is multidimensional");
  73. int size = array.Length;
  74. if (index > size)
  75. throw new ArgumentException ("index is larger than array size");
  76. if (index + Count > size)
  77. throw new ArgumentException ("Copy will overlflow array");
  78. DoCopy (array, index);
  79. }
  80. private void DoCopy (Array array, int index)
  81. {
  82. foreach (DictionaryEntry de in hashtable)
  83. array.SetValue (de, index++);
  84. }
  85. public IDictionaryEnumerator GetEnumerator ()
  86. {
  87. return hashtable.GetEnumerator ();
  88. }
  89. protected virtual void OnClear ()
  90. {
  91. }
  92. protected virtual void OnClearComplete ()
  93. {
  94. }
  95. protected virtual object OnGet (object key, object current_value)
  96. {
  97. return current_value;
  98. }
  99. protected virtual void OnInsert (object key, object current_value)
  100. {
  101. }
  102. protected virtual void OnInsertComplete (object key, object current_value)
  103. {
  104. }
  105. protected virtual void OnSet (object key, object current_value, object new_value)
  106. {
  107. }
  108. protected virtual void OnSetComplete (object key, object current_value, object new_value)
  109. {
  110. }
  111. protected virtual void OnRemove (object key, object current_value)
  112. {
  113. }
  114. protected virtual void OnRemoveComplete (object key, object current_value)
  115. {
  116. }
  117. protected virtual void OnValidate (object key, object current_value)
  118. {
  119. }
  120. bool IDictionary.IsFixedSize {
  121. get {
  122. return false;
  123. }
  124. }
  125. bool IDictionary.IsReadOnly {
  126. get {
  127. return false;
  128. }
  129. }
  130. object IDictionary.this [object key] {
  131. get {
  132. OnGet (key, hashtable [key]);
  133. object value = hashtable [key];
  134. return value;
  135. }
  136. set {
  137. OnValidate (key, value);
  138. object current_value = hashtable [key];
  139. OnSet (key, current_value, value);
  140. hashtable [key] = value;
  141. try {
  142. OnSetComplete (key, current_value, value);
  143. } catch {
  144. hashtable [key] = current_value;
  145. throw;
  146. }
  147. }
  148. }
  149. ICollection IDictionary.Keys {
  150. get {
  151. return hashtable.Keys;
  152. }
  153. }
  154. ICollection IDictionary.Values {
  155. get {
  156. return hashtable.Values;
  157. }
  158. }
  159. void IDictionary.Add (object key, object value)
  160. {
  161. OnValidate (key, value);
  162. OnInsert (key, value);
  163. hashtable.Add (key, value);
  164. try {
  165. OnInsertComplete (key, value);
  166. } catch {
  167. hashtable.Remove (key);
  168. throw;
  169. }
  170. }
  171. void IDictionary.Remove (object key)
  172. {
  173. object value = hashtable [key];
  174. OnValidate (key, value);
  175. OnRemove (key, value);
  176. hashtable.Remove (key);
  177. OnRemoveComplete (key, value);
  178. }
  179. bool IDictionary.Contains (object key)
  180. {
  181. return hashtable.Contains (key);
  182. }
  183. bool ICollection.IsSynchronized {
  184. get {
  185. return hashtable.IsSynchronized;
  186. }
  187. }
  188. object ICollection.SyncRoot {
  189. get {
  190. return hashtable.SyncRoot;
  191. }
  192. }
  193. IEnumerator IEnumerable.GetEnumerator ()
  194. {
  195. return hashtable.GetEnumerator ();
  196. }
  197. }
  198. }