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;
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 sealed class StaticItem {
37 object this_lock = new object();
42 public StaticItem (Type type)
47 public StaticItem (StaticItem item)
49 this.type = item.type;
52 public object Instance {
56 instance = Activator.CreateInstance (type);
64 // Needs to hold object items that can be latebound and can be serialized
66 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
68 public HttpStaticObjectsCollection ()
70 _Objects = new Hashtable ();
73 // this ctor has no security requirements and is used when creating HttpApplicationState
74 internal HttpStaticObjectsCollection (HttpApplicationState appstate)
76 _Objects = new Hashtable ();
79 public object GetObject (string name)
84 public IEnumerator GetEnumerator ()
86 return _Objects.GetEnumerator ();
89 public void CopyTo (Array array, int index)
91 _Objects.CopyTo (array, index);
94 internal IDictionary GetObjects ()
99 public object this [string name] {
101 StaticItem item = _Objects [name] as StaticItem;
105 return item.Instance;
110 get { return _Objects.Count; }
113 public bool IsReadOnly {
117 public bool IsSynchronized {
118 get { return false; }
121 [MonoTODO ("Not implemented")]
122 public bool NeverAccessed {
123 get { throw new NotImplementedException (); }
126 public object SyncRoot {
130 internal HttpStaticObjectsCollection Clone ()
132 HttpStaticObjectsCollection coll = new HttpStaticObjectsCollection ();
133 coll._Objects = new Hashtable ();
134 foreach (string key in _Objects.Keys) {
135 StaticItem item = new StaticItem ((StaticItem) _Objects [key]);
136 coll._Objects [key] = item;
142 internal void Add (ObjectTagBuilder tag)
144 _Objects.Add (tag.ObjectID, new StaticItem (tag.Type));
147 void Set (string name, object obj)
149 _Objects [name] = obj;
152 public void Serialize (BinaryWriter writer)
154 writer.Write (_Objects.Count);
155 foreach (string key in _Objects.Keys) {
157 System.Web.Util.AltSerialization.Serialize (writer, _Objects [key]);
161 public static HttpStaticObjectsCollection Deserialize (BinaryReader reader)
163 HttpStaticObjectsCollection result = new HttpStaticObjectsCollection ();
164 for (int i = reader.ReadInt32 (); i > 0; i--)
165 result.Set (reader.ReadString (),
166 System.Web.Util.AltSerialization.Deserialize (reader));
171 internal byte [] ToByteArray ()
173 MemoryStream stream = null;
175 stream = new MemoryStream ();
176 Serialize (new BinaryWriter (stream));
177 return stream.GetBuffer ();
186 internal static HttpStaticObjectsCollection FromByteArray (byte [] data)
188 HttpStaticObjectsCollection objs = null;
189 MemoryStream stream = null;
191 stream = new MemoryStream (data);
192 objs = Deserialize (new BinaryReader (stream));