2 // System.Web.UI.StateBag
5 // Ben Maurer <bmaurer@novell.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections;
30 using System.Collections.Specialized;
31 using System.Security.Permissions;
33 namespace System.Web.UI {
35 // CAS - no InheritanceDemand here as the class is sealed
36 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37 public sealed class StateBag : IDictionary, IStateManager {
42 public StateBag (bool ignoreCase)
44 ht = new HybridDictionary (ignoreCase);
47 public StateBag () : this (false)
51 void IStateManager.LoadViewState (object savedState)
53 LoadViewState (savedState);
56 object IStateManager.SaveViewState ()
58 return SaveViewState ();
62 void IStateManager.TrackViewState ()
67 bool IStateManager.IsTrackingViewState {
73 internal bool IsTrackingViewState {
80 internal void LoadViewState (object savedState)
82 if (savedState == null)
85 foreach (DictionaryEntry de in (Hashtable) savedState)
86 Add ((string) de.Key, de.Value);
89 internal object SaveViewState ()
93 foreach (DictionaryEntry de in ht) {
94 StateItem si = (StateItem) de.Value;
98 h.Add (de.Key, si.Value);
105 internal void TrackViewState ()
110 public StateItem Add (string key, object value)
112 StateItem si = ht [key] as StateItem;
114 ht [key] = si = new StateItem (value);
121 internal string GetString (string key, string def)
123 string s = (string) this [key];
124 return s == null ? def : s;
127 internal bool GetBool (string key, bool def)
129 object o = this [key];
130 return o == null ? def : (bool) o;
133 internal char GetChar (string key, char def)
135 object o = this [key];
136 return o == null ? def : (char) o;
139 internal int GetInt (string key, int def)
141 object o = this [key];
142 return o == null ? def : (int) o;
145 internal short GetShort (string key, short def)
147 object o = this [key];
148 return o == null ? def : (short) o;
156 public IDictionaryEnumerator GetEnumerator ()
158 return ht.GetEnumerator ();
161 IEnumerator IEnumerable.GetEnumerator ()
163 return GetEnumerator ();
166 public bool IsItemDirty (string key)
168 StateItem si = ht [key] as StateItem;
169 return si != null && si.IsDirty;
172 public void Remove (string key)
177 public void SetItemDirty (string key, bool dirty)
179 StateItem si = (StateItem) ht [key];
190 public object this [string key] {
192 StateItem i = ht [key] as StateItem;
199 if (value == null && ! IsTrackingViewState)
206 public ICollection Keys {
213 public ICollection Values {
219 void IDictionary.Add (object key, object value)
221 Add ((string) key, value);
224 void IDictionary.Remove (object key)
226 Remove ((string) key);
229 void ICollection.CopyTo (Array array, int index)
231 ht.CopyTo (array, index);
234 bool IDictionary.Contains (object key)
236 return ht.Contains (key);
239 bool ICollection.IsSynchronized {
245 object ICollection.SyncRoot {
251 object IDictionary.this [object key] {
253 return this [(string) key];
257 this [(string) key] = value;
261 bool IDictionary.IsFixedSize {
267 bool IDictionary.IsReadOnly {
274 public void SetDirty (bool dirty)
276 foreach (DictionaryEntry de in ht) {
277 StateItem si = (StateItem) de.Value;