SessionStateItemCollection.cs 3.3 KB

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