SessionStateItemCollection.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. bool is_dirty;
  40. static bool IsMutable (object o)
  41. {
  42. return (o != null && Type.GetTypeCode(o.GetType()) == TypeCode.Object);
  43. }
  44. public SessionStateItemCollection ()
  45. {
  46. }
  47. internal SessionStateItemCollection (int capacity)
  48. : base (capacity)
  49. {
  50. }
  51. public bool Dirty {
  52. get { return is_dirty; }
  53. set { is_dirty = value; }
  54. }
  55. public object this [int index] {
  56. get {
  57. object o = BaseGet (index);
  58. if (IsMutable (o))
  59. is_dirty = true;
  60. return o;
  61. }
  62. set {
  63. BaseSet (index, value);
  64. is_dirty = true;
  65. }
  66. }
  67. public object this [string name] {
  68. get {
  69. object o = BaseGet (name);
  70. if (IsMutable (o))
  71. is_dirty = true;
  72. return o;
  73. }
  74. set {
  75. BaseSet (name, value);
  76. is_dirty = true;
  77. }
  78. }
  79. // Todo: why override this?
  80. public override KeysCollection Keys {
  81. get { return base.Keys; }
  82. }
  83. public void Clear ()
  84. {
  85. if (Count > 0) {
  86. BaseClear ();
  87. is_dirty = true;
  88. }
  89. }
  90. public static SessionStateItemCollection Deserialize (BinaryReader reader)
  91. {
  92. int i = reader.ReadInt32 ();
  93. SessionStateItemCollection ret = new SessionStateItemCollection (i);
  94. for (; i > 0; i--)
  95. ret [reader.ReadString ()] =
  96. System.Web.Util.AltSerialization.Deserialize (reader);
  97. return ret;
  98. }
  99. public void Serialize (BinaryWriter writer) {
  100. writer.Write (Count);
  101. foreach (string key in base.Keys) {
  102. writer.Write (key);
  103. System.Web.Util.AltSerialization.Serialize (writer, BaseGet (key));
  104. }
  105. }
  106. // Todo: why override this?
  107. public override IEnumerator GetEnumerator ()
  108. {
  109. return base.GetEnumerator ();
  110. }
  111. public void Remove (string name)
  112. {
  113. BaseRemove (name);
  114. is_dirty = true;
  115. }
  116. public void RemoveAt (int index)
  117. {
  118. BaseRemoveAt (index);
  119. is_dirty = true;
  120. }
  121. }
  122. }
  123. #endif