StateBag.cs 4.7 KB

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