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 {
36 object this_lock = new object();
41 public StaticItem (Type type)
46 public StaticItem (StaticItem item)
48 this.type = item.type;
51 public object Instance {
55 instance = Activator.CreateInstance (type);
63 // Needs to hold object items that can be latebound and can be serialized
65 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
67 public HttpStaticObjectsCollection ()
69 _Objects = new Hashtable ();
72 // this ctor has no security requirements and is used when creating HttpApplicationState
73 internal HttpStaticObjectsCollection (HttpApplicationState appstate)
75 _Objects = new Hashtable ();
78 public object GetObject (string name)
83 public IEnumerator GetEnumerator ()
85 return _Objects.GetEnumerator ();
88 public void CopyTo (Array array, int index)
90 _Objects.CopyTo (array, index);
93 internal IDictionary GetObjects ()
98 public object this [string name] {
100 StaticItem item = _Objects [name] as StaticItem;
104 return item.Instance;
109 get { return _Objects.Count; }
112 public bool IsReadOnly {
116 public bool IsSynchronized {
117 get { return false; }
121 [MonoTODO ("Not implemented")]
122 public bool NeverAccessed {
123 get { throw new NotImplementedException (); }
127 public object SyncRoot {
131 internal HttpStaticObjectsCollection Clone ()
133 HttpStaticObjectsCollection coll = new HttpStaticObjectsCollection ();
134 coll._Objects = new Hashtable ();
135 foreach (string key in _Objects.Keys) {
136 StaticItem item = new StaticItem ((StaticItem) _Objects [key]);
137 coll._Objects [key] = item;
143 internal void Add (ObjectTagBuilder tag)
145 _Objects.Add (tag.ObjectID, new StaticItem (tag.Type));
148 void Set (string name, object obj)
150 _Objects [name] = obj;
154 public void Serialize (BinaryWriter writer)
156 internal void Serialize (BinaryWriter writer)
159 writer.Write (_Objects.Count);
160 foreach (string key in _Objects.Keys) {
162 System.Web.Util.AltSerialization.Serialize (writer, _Objects [key]);
167 public static HttpStaticObjectsCollection Deserialize (BinaryReader reader)
169 internal static HttpStaticObjectsCollection Deserialize (BinaryReader reader)
172 HttpStaticObjectsCollection result = new HttpStaticObjectsCollection ();
173 for (int i = reader.ReadInt32 (); i > 0; i--)
174 result.Set (reader.ReadString (),
175 System.Web.Util.AltSerialization.Deserialize (reader));
180 internal byte [] ToByteArray ()
182 MemoryStream stream = null;
184 stream = new MemoryStream ();
185 Serialize (new BinaryWriter (stream));
186 return stream.GetBuffer ();
195 internal static HttpStaticObjectsCollection FromByteArray (byte [] data)
197 HttpStaticObjectsCollection objs = null;
198 MemoryStream stream = null;
200 stream = new MemoryStream (data);
201 objs = Deserialize (new BinaryReader (stream));