| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- //
- // System.Web.SessionState.SessionDictionary
- //
- // Authors:
- // Gonzalo Paniagua Javier ([email protected])
- //
- // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
- //
- using System;
- using System.IO;
- using System.Collections;
- using System.Collections.Specialized;
- using System.Runtime.Serialization.Formatters.Binary;
- namespace System.Web.SessionState {
- internal class SessionDictionary : NameObjectCollectionBase
- {
- static ArrayList types;
- bool _dirty;
- static SessionDictionary ()
- {
- types = new ArrayList ();
- types.Add ("");
- types.Add (typeof (string));
- types.Add (typeof (int));
- types.Add (typeof (bool));
- types.Add (typeof (DateTime));
- types.Add (typeof (Decimal));
- types.Add (typeof (Byte));
- types.Add (typeof (Char));
- types.Add (typeof (Single));
- types.Add (typeof (Double));
- types.Add (typeof (short));
- types.Add (typeof (long));
- types.Add (typeof (ushort));
- types.Add (typeof (uint));
- types.Add (typeof (ulong));
- }
- public SessionDictionary ()
- {
- }
- internal void Clear ()
- {
- _dirty = true;
- BaseClear ();
- }
- internal string GetKey (int index)
- {
- return BaseGetKey (index);
- }
- internal static bool IsInmutable (object o)
- {
- Type t = o.GetType ();
- return (t == typeof (string) || t.IsPrimitive);
- }
- internal void Remove (string s)
- {
- BaseRemove (s);
- _dirty = true;
- }
- internal void RemoveAt (int index)
- {
- BaseRemoveAt (index);
- _dirty = true;
- }
- internal void Serialize (BinaryWriter w)
- {
- w.Write (Count);
- foreach (string key in Keys) {
- w.Write (key);
- object value = BaseGet (key);
- if (value == null) {
- w.Write (16); // types.Count + 1
- continue;
- }
- SerializeByType (w, value);
- }
- }
- static void SerializeByType (BinaryWriter w, object value)
- {
- Type type = value.GetType ();
- int i = types.IndexOf (type);
- if (i == -1) {
- w.Write (15); // types.Count
- BinaryFormatter bf = new BinaryFormatter ();
- bf.Serialize (w.BaseStream, value);
- return;
- }
- w.Write (i);
- switch (i) {
- case 1:
- w.Write ((string) value);
- break;
- case 2:
- w.Write ((int) value);
- break;
- case 3:
- w.Write ((bool) value);
- break;
- case 4:
- w.Write (((DateTime) value).Ticks);
- break;
- case 5:
- w.Write ((decimal) value);
- break;
- case 6:
- w.Write ((byte) value);
- break;
- case 7:
- w.Write ((char) value);
- break;
- case 8:
- w.Write ((float) value);
- break;
- case 9:
- w.Write ((double) value);
- break;
- case 10:
- w.Write ((short) value);
- break;
- case 11:
- w.Write ((long) value);
- break;
- case 12:
- w.Write ((ushort) value);
- break;
- case 13:
- w.Write ((uint) value);
- break;
- case 14:
- w.Write ((ulong) value);
- break;
- }
- }
- internal static SessionDictionary Deserialize (BinaryReader r)
- {
- SessionDictionary result = new SessionDictionary ();
- for (int i = r.ReadInt32 (); i > 0; i--) {
- string key = r.ReadString ();
- int index = r.ReadInt32 ();
- if (index == 16)
- result [key] = null;
- else
- result [key] = DeserializeFromIndex (index, r);
- }
- return result;
- }
- static object DeserializeFromIndex (int index, BinaryReader r)
- {
- if (index == 15){
- BinaryFormatter bf = new BinaryFormatter ();
- return bf.Deserialize (r.BaseStream);
- }
- object value = null;
- switch (index) {
- case 1:
- value = r.ReadString ();
- break;
- case 2:
- value = r.ReadInt32 ();
- break;
- case 3:
- value = r.ReadBoolean ();
- break;
- case 4:
- value = new DateTime (r.ReadInt64 ());
- break;
- case 5:
- value = r.ReadDecimal ();
- break;
- case 6:
- value = r.ReadByte ();
- break;
- case 7:
- value = r.ReadChar ();
- break;
- case 8:
- value = r.ReadSingle ();
- break;
- case 9:
- value = r.ReadDouble ();
- break;
- case 10:
- value = r.ReadInt16 ();
- break;
- case 11:
- value = r.ReadInt64 ();
- break;
- case 12:
- value = r.ReadUInt16 ();
- break;
- case 13:
- value = r.ReadUInt32 ();
- break;
- case 14:
- value = r.ReadUInt64 ();
- break;
- }
- return value;
- }
- internal bool Dirty
- {
- get { return _dirty; }
- set { _dirty = value; }
- }
- internal object this [string s]
- {
- get { return BaseGet (s); }
- set {
- BaseSet (s, value);
- _dirty = true;
- }
- }
- public object this [int index]
- {
- get { return BaseGet (index); }
- set {
- BaseSet (index, value);
- _dirty = true;
- }
- }
- }
- }
|