StateManagedCollection.cs 6.4 KB

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