StateManagedCollection.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // System.Web.UI.StateManagedCollection
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2003 Ben Maurer
  8. //
  9. #if NET_1_2
  10. using System.Collections;
  11. using System.Collections.Specialized;
  12. using System.Text;
  13. namespace System.Web.UI {
  14. public abstract class StateManagedCollection : IList, IStateManager {
  15. protected abstract object CreateKnownType (int index);
  16. protected abstract void SetDirtyObject (object o);
  17. protected virtual Type [] GetKnownTypes ()
  18. {
  19. return null;
  20. }
  21. #region OnXXX
  22. protected virtual void OnClear ()
  23. {
  24. }
  25. protected virtual void OnClearComplete ()
  26. {
  27. }
  28. protected virtual void OnInsert (int index, object value)
  29. {
  30. }
  31. protected virtual void OnInsertComplete (int index, object value)
  32. {
  33. }
  34. protected virtual void OnRemove (int index, object value)
  35. {
  36. }
  37. protected virtual void OnRemoveComplete (int index, object value)
  38. {
  39. }
  40. protected virtual void OnValidate (object value)
  41. {
  42. if (value == null)
  43. throw new ArgumentNullException ("value");
  44. }
  45. #endregion
  46. #region IStateManager
  47. void IStateManager.LoadViewState (object savedState)
  48. {
  49. int pos = -1;
  50. foreach (Pair p in (ArrayList)savedState) {
  51. pos ++;
  52. if (p == null)
  53. continue;
  54. IStateManager itm;
  55. if (p.Second is Type)
  56. itm = (IStateManager) Activator.CreateInstance ((Type) p.Second);
  57. else
  58. itm = (IStateManager) CreateKnownType ((int) p.Second);
  59. itm.LoadViewState (p.First);
  60. if (pos >= Count)
  61. items.Add (itm);
  62. else
  63. items [pos] = itm;
  64. }
  65. }
  66. object IStateManager.SaveViewState ()
  67. {
  68. ArrayList saved = new ArrayList ();
  69. Type [] knownTypes = GetKnownTypes ();
  70. foreach (IStateManager itm in items) {
  71. object state = itm.SaveViewState ();
  72. if (state == null && !saveEverything) {
  73. saved.Add (null);
  74. continue;
  75. }
  76. Pair p = new Pair ();
  77. p.First = state;
  78. Type t = itm.GetType ();
  79. int idx = -1;
  80. if (knownTypes != null)
  81. idx = Array.IndexOf (knownTypes, t);
  82. if (idx != -1)
  83. p.Second = idx;
  84. else
  85. p.Second = t;
  86. saved.Add (p);
  87. }
  88. return saved;
  89. }
  90. void IStateManager.TrackViewState ()
  91. {
  92. isTrackingViewState = true;
  93. foreach (IStateManager i in items)
  94. i.TrackViewState ();
  95. }
  96. bool isTrackingViewState;
  97. bool IStateManager.IsTrackingViewState {
  98. get { return isTrackingViewState; }
  99. }
  100. #endregion
  101. #region ICollection, IList, IEnumerable
  102. public void Clear ()
  103. {
  104. this.OnClear ();
  105. items.Clear ();
  106. this.OnClearComplete ();
  107. SetSaveEverything ();
  108. }
  109. public int IndexOf (object o)
  110. {
  111. if (o == null)
  112. return -1;
  113. return items.IndexOf (o);
  114. }
  115. public bool Contains (object o)
  116. {
  117. return o != null && items.Contains (o);
  118. }
  119. public IEnumerator GetEnumerator ()
  120. {
  121. return items.GetEnumerator ();
  122. }
  123. void System.Collections.ICollection.CopyTo (Array array, int index)
  124. {
  125. items.CopyTo (array, index);
  126. }
  127. IEnumerator IEnumerable.GetEnumerator ()
  128. {
  129. return GetEnumerator ();
  130. }
  131. int IList.Add (object value)
  132. {
  133. OnValidate(value);
  134. if (isTrackingViewState) {
  135. ((IStateManager) value).TrackViewState ();
  136. SetDirtyObject (value);
  137. }
  138. OnInsert (-1, value);
  139. items.Add (value);
  140. OnInsertComplete (-1, value);
  141. return Count - 1;
  142. }
  143. void IList.Insert (int index, object value)
  144. {
  145. OnValidate(value);
  146. if (isTrackingViewState) {
  147. ((IStateManager) value).TrackViewState ();
  148. SetDirtyObject (value);
  149. }
  150. OnInsert (index, value);
  151. items.Insert (index, value);
  152. OnInsertComplete(index, value);
  153. SetSaveEverything ();
  154. }
  155. void IList.Remove (object value)
  156. {
  157. if (value == null)
  158. return;
  159. OnValidate (value);
  160. ((IList)this).RemoveAt (IndexOf (value));
  161. }
  162. void IList.RemoveAt (int index)
  163. {
  164. object o = items [index];
  165. OnRemove (index, o);
  166. items.RemoveAt (index);
  167. OnRemoveComplete(index, o);
  168. SetSaveEverything ();
  169. }
  170. void IList.Clear ()
  171. {
  172. this.Clear ();
  173. }
  174. bool IList.Contains (object value)
  175. {
  176. if (value == null)
  177. return false;
  178. OnValidate (value);
  179. return Contains (value);
  180. }
  181. int IList.IndexOf (object value)
  182. {
  183. if (value == null)
  184. return -1;
  185. OnValidate (value);
  186. return IndexOf (value);
  187. }
  188. public int Count {
  189. get { return items.Count; }
  190. }
  191. int ICollection.Count {
  192. get { return items.Count; }
  193. }
  194. bool ICollection.IsSynchronized {
  195. get { return false; }
  196. }
  197. object ICollection.SyncRoot {
  198. get { return this; }
  199. }
  200. bool IList.IsFixedSize {
  201. get { return false; }
  202. }
  203. bool IList.IsReadOnly {
  204. get { return false; }
  205. }
  206. object IList.this [int index] {
  207. get { return items [index]; }
  208. set {
  209. if (index < 0 || index >= Count)
  210. throw new ArgumentOutOfRangeException ("index");
  211. OnValidate (value);
  212. if (isTrackingViewState) {
  213. ((IStateManager) value).TrackViewState ();
  214. SetDirtyObject (value);
  215. }
  216. items [index] = value;
  217. }
  218. }
  219. #endregion
  220. ArrayList items = new ArrayList ();
  221. bool saveEverything = false;
  222. void SetSaveEverything ()
  223. {
  224. if (isTrackingViewState)
  225. saveEverything = true;
  226. }
  227. }
  228. }
  229. #endif