2 // System.Web.SessionState.SessionDictionary
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
12 using System.Collections;
13 using System.Collections.Specialized;
14 using System.Runtime.Serialization.Formatters.Binary;
16 namespace System.Web.SessionState {
17 internal class SessionDictionary : NameObjectCollectionBase
19 static ArrayList types;
22 static SessionDictionary ()
24 types = new ArrayList ();
26 types.Add (typeof (string));
27 types.Add (typeof (int));
28 types.Add (typeof (bool));
29 types.Add (typeof (DateTime));
30 types.Add (typeof (Decimal));
31 types.Add (typeof (Byte));
32 types.Add (typeof (Char));
33 types.Add (typeof (Single));
34 types.Add (typeof (Double));
35 types.Add (typeof (short));
36 types.Add (typeof (long));
37 types.Add (typeof (ushort));
38 types.Add (typeof (uint));
39 types.Add (typeof (ulong));
42 public SessionDictionary ()
46 internal void Clear ()
53 internal string GetKey (int index)
57 value = BaseGetKey (index);
62 internal static bool IsInmutable (object o)
64 Type t = o.GetType ();
65 return (t == typeof (string) || t.IsPrimitive);
68 internal void Remove (string s)
75 internal void RemoveAt (int index)
82 internal void Serialize (BinaryWriter w)
86 foreach (string key in Keys) {
88 object value = BaseGet (key);
90 w.Write (16); // types.Count + 1
94 SerializeByType (w, value);
99 static void SerializeByType (BinaryWriter w, object value)
101 Type type = value.GetType ();
102 int i = types.IndexOf (type);
104 w.Write (15); // types.Count
105 BinaryFormatter bf = new BinaryFormatter ();
106 bf.Serialize (w.BaseStream, value);
113 w.Write ((string) value);
116 w.Write ((int) value);
119 w.Write ((bool) value);
122 w.Write (((DateTime) value).Ticks);
125 w.Write ((decimal) value);
128 w.Write ((byte) value);
131 w.Write ((char) value);
134 w.Write ((float) value);
137 w.Write ((double) value);
140 w.Write ((short) value);
143 w.Write ((long) value);
146 w.Write ((ushort) value);
149 w.Write ((uint) value);
152 w.Write ((ulong) value);
157 internal static SessionDictionary Deserialize (BinaryReader r)
159 SessionDictionary result = new SessionDictionary ();
160 for (int i = r.ReadInt32 (); i > 0; i--) {
161 string key = r.ReadString ();
162 int index = r.ReadInt32 ();
166 result [key] = DeserializeFromIndex (index, r);
172 static object DeserializeFromIndex (int index, BinaryReader r)
175 BinaryFormatter bf = new BinaryFormatter ();
176 return bf.Deserialize (r.BaseStream);
182 value = r.ReadString ();
185 value = r.ReadInt32 ();
188 value = r.ReadBoolean ();
191 value = new DateTime (r.ReadInt64 ());
194 value = r.ReadDecimal ();
197 value = r.ReadByte ();
200 value = r.ReadChar ();
203 value = r.ReadSingle ();
206 value = r.ReadDouble ();
209 value = r.ReadInt16 ();
212 value = r.ReadInt64 ();
215 value = r.ReadUInt16 ();
218 value = r.ReadUInt32 ();
221 value = r.ReadUInt64 ();
231 get { return _dirty; }
232 set { _dirty = value; }
235 internal object this [string s]
253 public object this [int index]
264 BaseSet (index, value);