SessionStateItemCollection.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // System.Web.Compilation.SessionStateItemCollection
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2006 Marek Habersack
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using System.Collections.Specialized;
  33. using System.IO;
  34. using System.Web.Util;
  35. namespace System.Web.SessionState
  36. {
  37. public sealed class SessionStateItemCollection : NameObjectCollectionBase, ISessionStateItemCollection, ICollection, IEnumerable
  38. {
  39. static Dictionary <Type, bool> immutables;
  40. bool is_dirty;
  41. static SessionStateItemCollection ()
  42. {
  43. immutables = new Dictionary <Type, bool> (14);
  44. Type type = typeof(string);
  45. immutables.Add (type, true);
  46. type = typeof(int);
  47. immutables.Add (type, true);
  48. type = typeof(bool);
  49. immutables.Add (type, true);
  50. type = typeof(decimal);
  51. immutables.Add (type, true);
  52. type = typeof(byte);
  53. immutables.Add (type, true);
  54. type = typeof(char);
  55. immutables.Add (type, true);
  56. type = typeof(float);
  57. immutables.Add (type, true);
  58. type = typeof(double);
  59. immutables.Add (type, true);
  60. type = typeof(sbyte);
  61. immutables.Add (type, true);
  62. type = typeof(short);
  63. immutables.Add (type, true);
  64. type = typeof(long);
  65. immutables.Add (type, true);
  66. type = typeof(ushort);
  67. immutables.Add (type, true);
  68. type = typeof(uint);
  69. immutables.Add (type, true);
  70. type = typeof(ulong);
  71. immutables.Add (type, true);
  72. }
  73. static bool IsMutable (object o)
  74. {
  75. return (immutables.ContainsKey (o.GetType ()) == false);
  76. }
  77. public SessionStateItemCollection ()
  78. {
  79. }
  80. internal SessionStateItemCollection (int capacity)
  81. : base (capacity)
  82. {
  83. }
  84. public bool Dirty {
  85. get { return is_dirty; }
  86. set { is_dirty = value; }
  87. }
  88. public object this [int index] {
  89. get {
  90. object o = BaseGet (index);
  91. if (o != null && IsMutable (o))
  92. is_dirty = true;
  93. return o;
  94. }
  95. set {
  96. BaseSet (index, value);
  97. is_dirty = true;
  98. }
  99. }
  100. public object this [string name] {
  101. get {
  102. object o = BaseGet (name);
  103. if (o != null && IsMutable (o))
  104. is_dirty = true;
  105. return o;
  106. }
  107. set {
  108. BaseSet (name, value);
  109. is_dirty = true;
  110. }
  111. }
  112. // Todo: why override this?
  113. public override KeysCollection Keys {
  114. get { return base.Keys; }
  115. }
  116. public void Clear ()
  117. {
  118. BaseClear ();
  119. is_dirty = true;
  120. }
  121. public static SessionStateItemCollection Deserialize (BinaryReader reader)
  122. {
  123. int i = reader.ReadInt32 ();
  124. SessionStateItemCollection ret = new SessionStateItemCollection (i);
  125. while (i > 0) {
  126. i--;
  127. string key = reader.ReadString ();
  128. int index = reader.ReadInt32 ();
  129. if (index == AltSerialization.NullIndex)
  130. ret [key] = null;
  131. else
  132. ret [key] = AltSerialization.DeserializeFromIndex (index, reader);
  133. }
  134. return ret;
  135. }
  136. public void Serialize (BinaryWriter writer)
  137. {
  138. lock (this) {
  139. writer.Write (Count);
  140. foreach (string key in base.Keys) {
  141. object val = BaseGet (key);
  142. if (val == null) {
  143. writer.Write (AltSerialization.NullIndex);
  144. continue;
  145. }
  146. AltSerialization.SerializeByType (writer, val);
  147. }
  148. }
  149. }
  150. // Todo: why override this?
  151. public override IEnumerator GetEnumerator ()
  152. {
  153. return base.GetEnumerator ();
  154. }
  155. public void Remove (string name)
  156. {
  157. BaseRemove (name);
  158. is_dirty = true;
  159. }
  160. public void RemoveAt (int index)
  161. {
  162. BaseRemoveAt (index);
  163. is_dirty = true;
  164. }
  165. }
  166. }
  167. #endif