StateManagedCollection.cs 6.2 KB

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