3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 using System.Collections;
25 using System.Security.Permissions;
28 namespace System.Web {
30 // CAS - no InheritanceDemand here as the class is sealed
31 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
32 public sealed class HttpStaticObjectsCollection : ICollection, IEnumerable {
33 private Hashtable _Objects;
39 public StaticItem (Type type)
44 public StaticItem (StaticItem item)
46 this.type = item.type;
49 public object Instance {
53 instance = Activator.CreateInstance (type);
61 // Needs to hold object items that can be latebound and can be serialized
63 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
65 public HttpStaticObjectsCollection ()
67 _Objects = new Hashtable ();
70 // this ctor has no security requirements and is used when creating HttpApplicationState
71 internal HttpStaticObjectsCollection (HttpApplicationState appstate)
73 _Objects = new Hashtable ();
76 public object GetObject (string name)
81 public IEnumerator GetEnumerator ()
83 return _Objects.GetEnumerator ();
86 public void CopyTo (Array array, int index)
88 _Objects.CopyTo (array, index);
91 internal IDictionary GetObjects ()
96 public object this [string name] {
98 StaticItem item = _Objects [name] as StaticItem;
102 return item.Instance;
107 get { return _Objects.Count; }
110 public bool IsReadOnly {
114 public bool IsSynchronized {
115 get { return false; }
119 [MonoTODO ("Not implemented")]
120 public bool NeverAccessed {
121 get { throw new NotImplementedException (); }
125 public object SyncRoot {
129 internal HttpStaticObjectsCollection Clone ()
131 HttpStaticObjectsCollection coll = new HttpStaticObjectsCollection ();
132 coll._Objects = new Hashtable ();
133 foreach (string key in _Objects.Keys) {
134 StaticItem item = new StaticItem ((StaticItem) _Objects [key]);
135 coll._Objects [key] = item;
141 internal void Add (ObjectTagBuilder tag)
143 _Objects.Add (tag.ObjectID, new StaticItem (tag.Type));
146 private void Set (string name, object obj)
148 _Objects [name] = obj;
152 public void Serialize (BinaryWriter writer)
154 internal void Serialize (BinaryWriter writer)
158 writer.Write (Count);
159 foreach (string key in _Objects.Keys) {
161 object value = _Objects [key];
163 writer.Write (System.Web.Util.AltSerialization.NullIndex);
167 System.Web.Util.AltSerialization.SerializeByType (writer, value);
173 public static HttpStaticObjectsCollection Deserialize (BinaryReader reader)
175 internal static HttpStaticObjectsCollection Deserialize (BinaryReader reader)
178 HttpStaticObjectsCollection result = new HttpStaticObjectsCollection ();
179 for (int i = reader.ReadInt32 (); i > 0; i--) {
180 string key = reader.ReadString ();
181 int index = reader.ReadInt32 ();
182 if (index == System.Web.Util.AltSerialization.NullIndex)
183 result.Set (key, null);
185 result.Set (key, System.Web.Util.AltSerialization.DeserializeFromIndex (index, reader));
191 internal byte [] ToByteArray ()
193 MemoryStream stream = null;
195 stream = new MemoryStream ();
196 Serialize (new BinaryWriter (stream));
197 return stream.GetBuffer ();
206 internal static HttpStaticObjectsCollection FromByteArray (byte [] data)
208 HttpStaticObjectsCollection objs = null;
209 MemoryStream stream = null;
211 stream = new MemoryStream (data);
212 objs = Deserialize (new BinaryReader (stream));