StateBag.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 HybridDictionary 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 ((StateItem) val).Value;
  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 = bag [key] as StateItem; //don't throw exception when null
  67. if(val == null) {
  68. if(value != null || marked) {
  69. val = new StateItem (value);
  70. bag.Add (key, val);
  71. }
  72. }
  73. else if (value == null && !marked)
  74. bag.Remove (key);
  75. else
  76. val.Value = value;
  77. if (val != null && marked) {
  78. val.IsDirty = true;
  79. }
  80. return val;
  81. }
  82. public void Clear ()
  83. {
  84. bag.Clear ();
  85. }
  86. public IDictionaryEnumerator GetEnumerator ()
  87. {
  88. return bag.GetEnumerator ();
  89. }
  90. public bool IsItemDirty (string key)
  91. {
  92. object o = bag [key];
  93. if (o is StateItem)
  94. return ((StateItem) o).IsDirty;
  95. return false;
  96. }
  97. public void Remove (string key)
  98. {
  99. bag.Remove (key);
  100. }
  101. /// <summary>
  102. /// Undocumented
  103. /// </summary>
  104. public void SetItemDirty (string key, bool dirty)
  105. {
  106. if (bag [key] is StateItem)
  107. ((StateItem) bag [key]).IsDirty = dirty;
  108. }
  109. internal bool IsTrackingViewState {
  110. get { return marked; }
  111. }
  112. internal void LoadViewState (object state)
  113. {
  114. if(state!=null) {
  115. Pair pair = (Pair) state;
  116. ArrayList keyList = (ArrayList) (pair.First);
  117. ArrayList valList = (ArrayList) (pair.Second);
  118. int valCount = valList.Count;
  119. for(int i = 0; i < keyList.Count; i++) {
  120. if (i < valCount)
  121. Add ((string) keyList [i], valList [i]);
  122. else
  123. Add ((string) keyList [i], null);
  124. }
  125. }
  126. }
  127. internal object SaveViewState ()
  128. {
  129. if(bag.Count > 0) {
  130. ArrayList keyList = null, valList = null;
  131. foreach (string key in bag.Keys) {
  132. StateItem item = (StateItem) bag [key];
  133. if (item.IsDirty) {
  134. if (keyList == null) {
  135. keyList = new ArrayList ();
  136. valList = new ArrayList ();
  137. }
  138. keyList.Add (key);
  139. valList.Add (item.Value);
  140. }
  141. }
  142. if (keyList!=null)
  143. return new Pair (keyList, valList);
  144. }
  145. return null;
  146. }
  147. internal void TrackViewState()
  148. {
  149. marked = true;
  150. }
  151. IEnumerator IEnumerable.GetEnumerator ()
  152. {
  153. return GetEnumerator ();
  154. }
  155. void IStateManager.LoadViewState (object savedState)
  156. {
  157. LoadViewState (savedState);
  158. }
  159. object IStateManager.SaveViewState ()
  160. {
  161. return SaveViewState ();
  162. }
  163. void IStateManager.TrackViewState ()
  164. {
  165. TrackViewState ();
  166. }
  167. bool IStateManager.IsTrackingViewState {
  168. get { return IsTrackingViewState; }
  169. }
  170. void ICollection.CopyTo (Array array, int index)
  171. {
  172. Values.CopyTo (array, index);
  173. }
  174. bool ICollection.IsSynchronized {
  175. get { return false; }
  176. }
  177. object ICollection.SyncRoot
  178. {
  179. get { return this; }
  180. }
  181. void IDictionary.Add (object key, object value)
  182. {
  183. Add ((string) key, value);
  184. }
  185. void IDictionary.Remove (object key)
  186. {
  187. Remove ((string) key);
  188. }
  189. bool IDictionary.Contains (object key)
  190. {
  191. return bag.Contains ((string) key);
  192. }
  193. bool IDictionary.IsFixedSize {
  194. get { return false; }
  195. }
  196. bool IDictionary.IsReadOnly {
  197. get { return false; }
  198. }
  199. }
  200. }