StateBag.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //
  2. // System.Web.UI.StateBag
  3. //
  4. // Author:
  5. // Ben Maurer <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Collections;
  29. using System.Collections.Specialized;
  30. using System.Security.Permissions;
  31. namespace System.Web.UI {
  32. // CAS - no InheritanceDemand here as the class is sealed
  33. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  34. public sealed class StateBag : IDictionary, IStateManager {
  35. HybridDictionary ht;
  36. bool track;
  37. public StateBag (bool ignoreCase)
  38. {
  39. ht = new HybridDictionary (ignoreCase);
  40. }
  41. public StateBag () : this (false)
  42. {
  43. }
  44. void IStateManager.LoadViewState (object savedState)
  45. {
  46. LoadViewState (savedState);
  47. }
  48. object IStateManager.SaveViewState ()
  49. {
  50. return SaveViewState ();
  51. }
  52. void IStateManager.TrackViewState ()
  53. {
  54. TrackViewState ();
  55. }
  56. bool IStateManager.IsTrackingViewState {
  57. get {
  58. return track;
  59. }
  60. }
  61. internal bool IsTrackingViewState {
  62. get {
  63. return track;
  64. }
  65. }
  66. internal void LoadViewState (object savedState)
  67. {
  68. if (savedState == null)
  69. return;
  70. foreach (DictionaryEntry de in (Hashtable) savedState)
  71. Add ((string) de.Key, de.Value);
  72. }
  73. internal object SaveViewState ()
  74. {
  75. Hashtable h = null;
  76. foreach (DictionaryEntry de in ht) {
  77. StateItem si = (StateItem) de.Value;
  78. if (si.IsDirty) {
  79. if (h == null)
  80. h = new Hashtable ();
  81. h.Add (de.Key, si.Value);
  82. }
  83. }
  84. return h;
  85. }
  86. internal void TrackViewState ()
  87. {
  88. track = true;
  89. }
  90. public StateItem Add (string key, object value)
  91. {
  92. StateItem si = ht [key] as StateItem;
  93. if (si == null)
  94. ht [key] = si = new StateItem (value);
  95. si.Value = value;
  96. si.IsDirty |= track;
  97. return si;
  98. }
  99. internal string GetString (string key, string def)
  100. {
  101. string s = (string) this [key];
  102. return s == null ? def : s;
  103. }
  104. internal bool GetBool (string key, bool def)
  105. {
  106. object o = this [key];
  107. return o == null ? def : (bool) o;
  108. }
  109. internal char GetChar (string key, char def)
  110. {
  111. object o = this [key];
  112. return o == null ? def : (char) o;
  113. }
  114. internal int GetInt (string key, int def)
  115. {
  116. object o = this [key];
  117. return o == null ? def : (int) o;
  118. }
  119. internal short GetShort (string key, short def)
  120. {
  121. object o = this [key];
  122. return o == null ? def : (short) o;
  123. }
  124. public void Clear ()
  125. {
  126. ht.Clear ();
  127. }
  128. public IDictionaryEnumerator GetEnumerator ()
  129. {
  130. return ht.GetEnumerator ();
  131. }
  132. IEnumerator IEnumerable.GetEnumerator ()
  133. {
  134. return GetEnumerator ();
  135. }
  136. public bool IsItemDirty (string key)
  137. {
  138. StateItem si = ht [key] as StateItem;
  139. return si != null && si.IsDirty;
  140. }
  141. public void Remove (string key)
  142. {
  143. ht.Remove (key);
  144. }
  145. public void SetItemDirty (string key, bool dirty)
  146. {
  147. StateItem si = (StateItem) ht [key];
  148. if (si != null)
  149. si.IsDirty = dirty;
  150. }
  151. public int Count {
  152. get {
  153. return ht.Count;
  154. }
  155. }
  156. public object this [string key] {
  157. get {
  158. StateItem i = ht [key] as StateItem;
  159. if (i != null)
  160. return i.Value;
  161. return null;
  162. }
  163. set {
  164. if (value == null && ! IsTrackingViewState)
  165. Remove (key);
  166. else
  167. Add (key, value);
  168. }
  169. }
  170. public ICollection Keys {
  171. get {
  172. return ht.Keys;
  173. }
  174. }
  175. public ICollection Values {
  176. get {
  177. return ht.Values;
  178. }
  179. }
  180. void IDictionary.Add (object key, object value)
  181. {
  182. Add ((string) key, value);
  183. }
  184. void IDictionary.Remove (object key)
  185. {
  186. Remove ((string) key);
  187. }
  188. void ICollection.CopyTo (Array array, int index)
  189. {
  190. ht.CopyTo (array, index);
  191. }
  192. bool IDictionary.Contains (object key)
  193. {
  194. return ht.Contains (key);
  195. }
  196. bool ICollection.IsSynchronized {
  197. get {
  198. return false;
  199. }
  200. }
  201. object ICollection.SyncRoot {
  202. get {
  203. return ht;
  204. }
  205. }
  206. object IDictionary.this [object key] {
  207. get {
  208. return this [(string) key];
  209. }
  210. set {
  211. this [(string) key] = value;
  212. }
  213. }
  214. bool IDictionary.IsFixedSize {
  215. get {
  216. return false;
  217. }
  218. }
  219. bool IDictionary.IsReadOnly {
  220. get {
  221. return false;
  222. }
  223. }
  224. #if NET_2_0
  225. public
  226. #else
  227. internal
  228. #endif
  229. void SetDirty (bool dirty)
  230. {
  231. foreach (DictionaryEntry de in ht) {
  232. StateItem si = (StateItem) de.Value;
  233. si.IsDirty = dirty;
  234. }
  235. }
  236. }
  237. }