SessionDictionary.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. lock (this)
  45. BaseClear ();
  46. }
  47. internal string GetKey (int index)
  48. {
  49. string value;
  50. lock (this)
  51. value = BaseGetKey (index);
  52. return value;
  53. }
  54. internal static bool IsInmutable (object o)
  55. {
  56. Type t = o.GetType ();
  57. return (t == typeof (string) || t.IsPrimitive);
  58. }
  59. internal void Remove (string s)
  60. {
  61. lock (this)
  62. BaseRemove (s);
  63. _dirty = true;
  64. }
  65. internal void RemoveAt (int index)
  66. {
  67. lock (this)
  68. BaseRemoveAt (index);
  69. _dirty = true;
  70. }
  71. internal void Serialize (BinaryWriter w)
  72. {
  73. lock (this) {
  74. w.Write (Count);
  75. foreach (string key in Keys) {
  76. w.Write (key);
  77. object value = BaseGet (key);
  78. if (value == null) {
  79. w.Write (16); // types.Count + 1
  80. continue;
  81. }
  82. SerializeByType (w, value);
  83. }
  84. }
  85. }
  86. static void SerializeByType (BinaryWriter w, object value)
  87. {
  88. Type type = value.GetType ();
  89. int i = types.IndexOf (type);
  90. if (i == -1) {
  91. w.Write (15); // types.Count
  92. BinaryFormatter bf = new BinaryFormatter ();
  93. bf.Serialize (w.BaseStream, value);
  94. return;
  95. }
  96. w.Write (i);
  97. switch (i) {
  98. case 1:
  99. w.Write ((string) value);
  100. break;
  101. case 2:
  102. w.Write ((int) value);
  103. break;
  104. case 3:
  105. w.Write ((bool) value);
  106. break;
  107. case 4:
  108. w.Write (((DateTime) value).Ticks);
  109. break;
  110. case 5:
  111. w.Write ((decimal) value);
  112. break;
  113. case 6:
  114. w.Write ((byte) value);
  115. break;
  116. case 7:
  117. w.Write ((char) value);
  118. break;
  119. case 8:
  120. w.Write ((float) value);
  121. break;
  122. case 9:
  123. w.Write ((double) value);
  124. break;
  125. case 10:
  126. w.Write ((short) value);
  127. break;
  128. case 11:
  129. w.Write ((long) value);
  130. break;
  131. case 12:
  132. w.Write ((ushort) value);
  133. break;
  134. case 13:
  135. w.Write ((uint) value);
  136. break;
  137. case 14:
  138. w.Write ((ulong) value);
  139. break;
  140. }
  141. }
  142. internal static SessionDictionary Deserialize (BinaryReader r)
  143. {
  144. SessionDictionary result = new SessionDictionary ();
  145. for (int i = r.ReadInt32 (); i > 0; i--) {
  146. string key = r.ReadString ();
  147. int index = r.ReadInt32 ();
  148. if (index == 16)
  149. result [key] = null;
  150. else
  151. result [key] = DeserializeFromIndex (index, r);
  152. }
  153. return result;
  154. }
  155. static object DeserializeFromIndex (int index, BinaryReader r)
  156. {
  157. if (index == 15){
  158. BinaryFormatter bf = new BinaryFormatter ();
  159. return bf.Deserialize (r.BaseStream);
  160. }
  161. object value = null;
  162. switch (index) {
  163. case 1:
  164. value = r.ReadString ();
  165. break;
  166. case 2:
  167. value = r.ReadInt32 ();
  168. break;
  169. case 3:
  170. value = r.ReadBoolean ();
  171. break;
  172. case 4:
  173. value = new DateTime (r.ReadInt64 ());
  174. break;
  175. case 5:
  176. value = r.ReadDecimal ();
  177. break;
  178. case 6:
  179. value = r.ReadByte ();
  180. break;
  181. case 7:
  182. value = r.ReadChar ();
  183. break;
  184. case 8:
  185. value = r.ReadSingle ();
  186. break;
  187. case 9:
  188. value = r.ReadDouble ();
  189. break;
  190. case 10:
  191. value = r.ReadInt16 ();
  192. break;
  193. case 11:
  194. value = r.ReadInt64 ();
  195. break;
  196. case 12:
  197. value = r.ReadUInt16 ();
  198. break;
  199. case 13:
  200. value = r.ReadUInt32 ();
  201. break;
  202. case 14:
  203. value = r.ReadUInt64 ();
  204. break;
  205. }
  206. return value;
  207. }
  208. internal bool Dirty
  209. {
  210. get { return _dirty; }
  211. set { _dirty = value; }
  212. }
  213. internal object this [string s]
  214. {
  215. get {
  216. object o;
  217. lock (this)
  218. o = BaseGet (s);
  219. return o;
  220. }
  221. set {
  222. lock (this)
  223. BaseSet (s, value);
  224. _dirty = true;
  225. }
  226. }
  227. public object this [int index]
  228. {
  229. get {
  230. object o;
  231. lock (this)
  232. o = BaseGet (index);
  233. return o;
  234. }
  235. set {
  236. lock (this)
  237. BaseSet (index, value);
  238. _dirty = true;
  239. }
  240. }
  241. }
  242. }