StateBag.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * Namespace: System.Web.UI
  3. * Class: StateBag
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Implementation: yes
  8. * Contact: <[email protected]>
  9. * Status: 100%
  10. *
  11. * (C) Gaurav Vaish (2001)
  12. */
  13. using System;
  14. using System.Web;
  15. using System.Collections;
  16. using System.Collections.Specialized;
  17. namespace System.Web.UI
  18. {
  19. public sealed class StateBag : IStateManager, IDictionary, ICollection, IEnumerable
  20. {
  21. private bool ignoreCase;
  22. private bool marked;
  23. private IDictionary bag;
  24. public StateBag (bool ignoreCase)
  25. {
  26. Initialize (ignoreCase);
  27. }
  28. public StateBag ()
  29. {
  30. Initialize (false);
  31. }
  32. private void Initialize (bool ignoreCase)
  33. {
  34. this.ignoreCase = ignoreCase;
  35. marked = false;
  36. bag = new HybridDictionary (ignoreCase);
  37. }
  38. public int Count {
  39. get { return bag.Count; }
  40. }
  41. public object this [string key] {
  42. get {
  43. if (key == null || key.Length == 0)
  44. throw new ArgumentException (HttpRuntime.FormatResourceString ("Key_Cannot_Be_Null"));
  45. object val = bag [key];
  46. if (val is StateItem)
  47. return val;
  48. return null; //
  49. }
  50. set { Add (key, value); }
  51. }
  52. object IDictionary.this [object key] {
  53. get { return this [(string) key] as object; }
  54. set { Add ((string) key, value); }
  55. }
  56. public ICollection Keys {
  57. get { return bag.Keys; }
  58. }
  59. public ICollection Values {
  60. get { return bag.Values; }
  61. }
  62. public StateItem Add (string key, object value)
  63. {
  64. if (key == null || key.Length == 0)
  65. throw new ArgumentException (HttpRuntime.FormatResourceString ("Key_Cannot_Be_Null"));
  66. StateItem val = null;
  67. if (bag [key] is StateItem)
  68. val = (StateItem) (bag [key]);
  69. if(val == null) {
  70. if(value != null || marked) {
  71. val = new StateItem (value);
  72. bag.Add (key, val);
  73. }
  74. } else {
  75. if (value != null && !marked) {
  76. bag.Remove (key);
  77. val.Value = value;
  78. }
  79. }
  80. if (val != null && marked)
  81. val.IsDirty = true;
  82. return val;
  83. }
  84. public void Clear ()
  85. {
  86. bag.Clear ();
  87. }
  88. public IDictionaryEnumerator GetEnumerator ()
  89. {
  90. return bag.GetEnumerator ();
  91. }
  92. public bool IsItemDirty (string key)
  93. {
  94. object o = bag [key];
  95. if (o is StateItem)
  96. return ((StateItem) o).IsDirty;
  97. return false;
  98. }
  99. public void Remove (string key)
  100. {
  101. bag.Remove (key);
  102. }
  103. /// <summary>
  104. /// Undocumented
  105. /// </summary>
  106. public void SetItemDirty (string key, bool dirty)
  107. {
  108. if (bag [key] is StateItem)
  109. ((StateItem) bag [key]).IsDirty = dirty;
  110. }
  111. internal bool IsTrackingViewState {
  112. get { return marked; }
  113. }
  114. internal void LoadViewState (object state)
  115. {
  116. if(state!=null) {
  117. Pair pair = (Pair) state;
  118. ArrayList keyList = (ArrayList) (pair.First);
  119. ArrayList valList = (ArrayList) (pair.Second);
  120. for(int i = 0; i < keyList.Count; i++)
  121. Add ((string) keyList [i], valList [i]);
  122. }
  123. }
  124. internal object SaveViewState ()
  125. {
  126. if(bag.Count > 0) {
  127. ArrayList keyList = null, valList = null;
  128. foreach(IDictionaryEnumerator current in bag) {
  129. StateItem item = (StateItem)current.Value;
  130. if (item.IsDirty) {
  131. if (keyList == null) {
  132. keyList = new ArrayList ();
  133. valList = new ArrayList ();
  134. }
  135. keyList.Add (current.Key);
  136. valList.Add (current.Value);
  137. }
  138. }
  139. if (keyList!=null)
  140. return new Pair (keyList, valList);
  141. }
  142. return null;
  143. }
  144. internal void TrackViewState()
  145. {
  146. marked = true;
  147. }
  148. IEnumerator IEnumerable.GetEnumerator ()
  149. {
  150. return GetEnumerator ();
  151. }
  152. void IStateManager.LoadViewState (object savedState)
  153. {
  154. LoadViewState (savedState);
  155. }
  156. object IStateManager.SaveViewState ()
  157. {
  158. return SaveViewState ();
  159. }
  160. void IStateManager.TrackViewState ()
  161. {
  162. TrackViewState ();
  163. }
  164. bool IStateManager.IsTrackingViewState {
  165. get { return IsTrackingViewState; }
  166. }
  167. void ICollection.CopyTo (Array array, int index)
  168. {
  169. Values.CopyTo (array, index);
  170. }
  171. bool ICollection.IsSynchronized {
  172. get { return false; }
  173. }
  174. object ICollection.SyncRoot
  175. {
  176. get { return this; }
  177. }
  178. void IDictionary.Add (object key, object value)
  179. {
  180. Add ((string) key, value);
  181. }
  182. void IDictionary.Remove (object key)
  183. {
  184. Remove ((string) key);
  185. }
  186. bool IDictionary.Contains (object key)
  187. {
  188. return bag.Contains ((string) key);
  189. }
  190. bool IDictionary.IsFixedSize {
  191. get { return false; }
  192. }
  193. bool IDictionary.IsReadOnly {
  194. get { return false; }
  195. }
  196. }
  197. }