StateBag.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. for(int i = 0; i < keyList.Count; i++)
  119. Add ((string) keyList [i], valList [i]);
  120. }
  121. }
  122. internal object SaveViewState ()
  123. {
  124. if(bag.Count > 0) {
  125. ArrayList keyList = null, valList = null;
  126. foreach (string key in bag.Keys) {
  127. StateItem item = (StateItem) bag [key];
  128. if (item.IsDirty) {
  129. if (keyList == null) {
  130. keyList = new ArrayList ();
  131. valList = new ArrayList ();
  132. }
  133. keyList.Add (key);
  134. valList.Add (item.Value);
  135. }
  136. }
  137. if (keyList!=null)
  138. return new Pair (keyList, valList);
  139. }
  140. return null;
  141. }
  142. internal void TrackViewState()
  143. {
  144. marked = true;
  145. }
  146. IEnumerator IEnumerable.GetEnumerator ()
  147. {
  148. return GetEnumerator ();
  149. }
  150. void IStateManager.LoadViewState (object savedState)
  151. {
  152. LoadViewState (savedState);
  153. }
  154. object IStateManager.SaveViewState ()
  155. {
  156. return SaveViewState ();
  157. }
  158. void IStateManager.TrackViewState ()
  159. {
  160. TrackViewState ();
  161. }
  162. bool IStateManager.IsTrackingViewState {
  163. get { return IsTrackingViewState; }
  164. }
  165. void ICollection.CopyTo (Array array, int index)
  166. {
  167. Values.CopyTo (array, index);
  168. }
  169. bool ICollection.IsSynchronized {
  170. get { return false; }
  171. }
  172. object ICollection.SyncRoot
  173. {
  174. get { return this; }
  175. }
  176. void IDictionary.Add (object key, object value)
  177. {
  178. Add ((string) key, value);
  179. }
  180. void IDictionary.Remove (object key)
  181. {
  182. Remove ((string) key);
  183. }
  184. bool IDictionary.Contains (object key)
  185. {
  186. return bag.Contains ((string) key);
  187. }
  188. bool IDictionary.IsFixedSize {
  189. get { return false; }
  190. }
  191. bool IDictionary.IsReadOnly {
  192. get { return false; }
  193. }
  194. }
  195. }