2 // System.Web.SessionState.SessionDictionary
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
34 using System.Collections.Specialized;
36 namespace System.Web.SessionState {
37 internal class SessionDictionary : NameObjectCollectionBase
41 public SessionDictionary ()
45 internal SessionDictionary Clone ()
47 SessionDictionary sess = new SessionDictionary ();
48 int last = sess.Count;
49 for (int i = 0; i < last; i++) {
50 string key = GetKey (i);
51 sess [key] = this [key];
57 internal void Clear ()
64 internal string GetKey (int index)
68 value = BaseGetKey (index);
73 internal static bool IsInmutable (object o)
75 Type t = o.GetType ();
76 return (t == typeof (string) || t.IsPrimitive);
79 internal void Remove (string s)
86 internal void RemoveAt (int index)
93 internal void Serialize (BinaryWriter w)
97 foreach (string key in Keys) {
99 object value = BaseGet (key);
101 w.Write (System.Web.Util.AltSerialization.NullIndex);
105 System.Web.Util.AltSerialization.SerializeByType (w, value);
110 internal static SessionDictionary Deserialize (BinaryReader r)
112 SessionDictionary result = new SessionDictionary ();
113 for (int i = r.ReadInt32 (); i > 0; i--) {
114 string key = r.ReadString ();
115 int index = r.ReadInt32 ();
116 if (index == System.Web.Util.AltSerialization.NullIndex)
119 result [key] = System.Web.Util.AltSerialization.DeserializeFromIndex (index, r);
127 get { return _dirty; }
128 set { _dirty = value; }
131 internal object this [string s]
144 object obj = BaseGet(s);
\r
145 if ((obj == null) && (value == null))
\r
154 public object this [int index]
166 object obj = BaseGet(index);
\r
167 if ((obj == null) && (value == null))
\r
169 BaseSet (index, value);
176 internal byte [] ToByteArray ()
178 MemoryStream stream = null;
180 stream = new MemoryStream ();
181 Serialize (new BinaryWriter (stream));
182 return stream.GetBuffer ();
191 internal static SessionDictionary FromByteArray (byte [] data)
193 SessionDictionary result = null;
194 MemoryStream stream = null;
196 stream = new MemoryStream (data);
197 result = Deserialize (new BinaryReader (stream));