SessionDictionary.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // System.Web.SessionState.SessionDictionary
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Collections;
  12. using System.Collections.Specialized;
  13. using System.Runtime.Serialization.Formatters.Binary;
  14. namespace System.Web.SessionState {
  15. internal class SessionDictionary : NameObjectCollectionBase
  16. {
  17. static ArrayList types;
  18. bool _dirty;
  19. static SessionDictionary ()
  20. {
  21. types = new ArrayList ();
  22. types.Add ("");
  23. types.Add (typeof (string));
  24. types.Add (typeof (int));
  25. types.Add (typeof (bool));
  26. types.Add (typeof (DateTime));
  27. types.Add (typeof (Decimal));
  28. types.Add (typeof (Byte));
  29. types.Add (typeof (Char));
  30. types.Add (typeof (Single));
  31. types.Add (typeof (Double));
  32. types.Add (typeof (short));
  33. types.Add (typeof (long));
  34. types.Add (typeof (ushort));
  35. types.Add (typeof (uint));
  36. types.Add (typeof (ulong));
  37. }
  38. public SessionDictionary ()
  39. {
  40. }
  41. internal void Clear ()
  42. {
  43. _dirty = true;
  44. BaseClear ();
  45. }
  46. internal string GetKey (int index)
  47. {
  48. return BaseGetKey (index);
  49. }
  50. internal static bool IsInmutable (object o)
  51. {
  52. Type t = o.GetType ();
  53. return (t == typeof (string) || t.IsPrimitive);
  54. }
  55. internal void Remove (string s)
  56. {
  57. BaseRemove (s);
  58. _dirty = true;
  59. }
  60. internal void RemoveAt (int index)
  61. {
  62. BaseRemoveAt (index);
  63. _dirty = true;
  64. }
  65. internal void Serialize (BinaryWriter w)
  66. {
  67. w.Write (Count);
  68. foreach (string key in Keys) {
  69. w.Write (key);
  70. object value = BaseGet (key);
  71. if (value == null) {
  72. w.Write (16); // types.Count + 1
  73. continue;
  74. }
  75. SerializeByType (w, value);
  76. }
  77. }
  78. static void SerializeByType (BinaryWriter w, object value)
  79. {
  80. Type type = value.GetType ();
  81. int i = types.IndexOf (type);
  82. if (i == -1) {
  83. w.Write (15); // types.Count
  84. BinaryFormatter bf = new BinaryFormatter ();
  85. bf.Serialize (w.BaseStream, value);
  86. return;
  87. }
  88. w.Write (i);
  89. switch (i) {
  90. case 1:
  91. w.Write ((string) value);
  92. break;
  93. case 2:
  94. w.Write ((int) value);
  95. break;
  96. case 3:
  97. w.Write ((bool) value);
  98. break;
  99. case 4:
  100. w.Write (((DateTime) value).Ticks);
  101. break;
  102. case 5:
  103. w.Write ((decimal) value);
  104. break;
  105. case 6:
  106. w.Write ((byte) value);
  107. break;
  108. case 7:
  109. w.Write ((char) value);
  110. break;
  111. case 8:
  112. w.Write ((float) value);
  113. break;
  114. case 9:
  115. w.Write ((double) value);
  116. break;
  117. case 10:
  118. w.Write ((short) value);
  119. break;
  120. case 11:
  121. w.Write ((long) value);
  122. break;
  123. case 12:
  124. w.Write ((ushort) value);
  125. break;
  126. case 13:
  127. w.Write ((uint) value);
  128. break;
  129. case 14:
  130. w.Write ((ulong) value);
  131. break;
  132. }
  133. }
  134. internal static SessionDictionary Deserialize (BinaryReader r)
  135. {
  136. SessionDictionary result = new SessionDictionary ();
  137. for (int i = r.ReadInt32 (); i > 0; i--) {
  138. string key = r.ReadString ();
  139. int index = r.ReadInt32 ();
  140. if (index == 16)
  141. result [key] = null;
  142. else
  143. result [key] = DeserializeFromIndex (index, r);
  144. }
  145. return result;
  146. }
  147. static object DeserializeFromIndex (int index, BinaryReader r)
  148. {
  149. if (index == 15){
  150. BinaryFormatter bf = new BinaryFormatter ();
  151. return bf.Deserialize (r.BaseStream);
  152. }
  153. object value = null;
  154. switch (index) {
  155. case 1:
  156. value = r.ReadString ();
  157. break;
  158. case 2:
  159. value = r.ReadInt32 ();
  160. break;
  161. case 3:
  162. value = r.ReadBoolean ();
  163. break;
  164. case 4:
  165. value = new DateTime (r.ReadInt64 ());
  166. break;
  167. case 5:
  168. value = r.ReadDecimal ();
  169. break;
  170. case 6:
  171. value = r.ReadByte ();
  172. break;
  173. case 7:
  174. value = r.ReadChar ();
  175. break;
  176. case 8:
  177. value = r.ReadSingle ();
  178. break;
  179. case 9:
  180. value = r.ReadDouble ();
  181. break;
  182. case 10:
  183. value = r.ReadInt16 ();
  184. break;
  185. case 11:
  186. value = r.ReadInt64 ();
  187. break;
  188. case 12:
  189. value = r.ReadUInt16 ();
  190. break;
  191. case 13:
  192. value = r.ReadUInt32 ();
  193. break;
  194. case 14:
  195. value = r.ReadUInt64 ();
  196. break;
  197. }
  198. return value;
  199. }
  200. internal bool Dirty
  201. {
  202. get { return _dirty; }
  203. set { _dirty = value; }
  204. }
  205. internal object this [string s]
  206. {
  207. get { return BaseGet (s); }
  208. set {
  209. BaseSet (s, value);
  210. _dirty = true;
  211. }
  212. }
  213. public object this [int index]
  214. {
  215. get { return BaseGet (index); }
  216. set {
  217. BaseSet (index, value);
  218. _dirty = true;
  219. }
  220. }
  221. }
  222. }