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;
24 using System.Collections.Generic;
26 using System.Security.Permissions;
31 // CAS - no InheritanceDemand here as the class is sealed
32 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
33 public sealed class HttpStaticObjectsCollection : ICollection, IEnumerable
35 sealed class StaticItem {
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 Dictionary <string, object> objects;
65 Dictionary <string, object> Objects {
68 objects = new Dictionary <string, object> (StringComparer.Ordinal);
74 // Needs to hold object items that can be latebound and can be serialized
75 public HttpStaticObjectsCollection ()
79 // this ctor has no security requirements and is used when creating HttpApplicationState
80 internal HttpStaticObjectsCollection (HttpApplicationState appstate)
84 public object GetObject (string name)
89 public IEnumerator GetEnumerator ()
91 return Objects.GetEnumerator ();
94 public void CopyTo (Array array, int index)
99 // Copied from Hashtable.CopyTo for the most part
101 throw new ArgumentNullException ("array");
104 throw new ArgumentOutOfRangeException ("index");
107 throw new ArgumentException ("array is multidimensional");
109 if ((array.Length > 0) && (index >= array.Length))
110 throw new ArgumentException ("index is equal to or greater than array.Length");
112 if (index + objects.Count > array.Length)
113 throw new ArgumentException ("Not enough room from index to end of array for this collection");
115 // We need to emulate Hashtable here, which uses DictionaryEntry for its items
116 foreach (var de in objects)
117 array.SetValue (new DictionaryEntry (de.Key, de.Value), index++);
120 internal IDictionary GetObjects ()
125 public object this [string name] {
130 StaticItem item = null;
132 if (Objects.TryGetValue (name, out o))
133 item = o as StaticItem;
138 return item.Instance;
147 return Objects.Count;
151 public bool IsReadOnly {
155 public bool IsSynchronized {
156 get { return false; }
159 [MonoTODO ("Not implemented")]
160 public bool NeverAccessed {
161 get { throw new NotImplementedException (); }
164 public object SyncRoot {
168 internal HttpStaticObjectsCollection Clone ()
170 HttpStaticObjectsCollection coll = new HttpStaticObjectsCollection ();
174 var collObjects = coll.Objects;
175 foreach (var de in objects) {
176 StaticItem item = new StaticItem ((StaticItem) de.Value);
177 collObjects [de.Key] = item;
183 internal void Add (ObjectTagBuilder tag)
185 Objects.Add (tag.ObjectID, new StaticItem (tag.Type));
188 void Set (string name, object obj)
190 Objects [name] = obj;
193 public void Serialize (BinaryWriter writer)
195 if (objects == null) {
200 writer.Write (objects.Count);
201 foreach (var de in objects) {
202 writer.Write (de.Key);
203 System.Web.Util.AltSerialization.Serialize (writer, de.Value);
207 public static HttpStaticObjectsCollection Deserialize (BinaryReader reader)
209 HttpStaticObjectsCollection result = new HttpStaticObjectsCollection ();
210 for (int i = reader.ReadInt32 (); i > 0; i--)
211 result.Set (reader.ReadString (), System.Web.Util.AltSerialization.Deserialize (reader));
216 internal byte [] ToByteArray ()
218 MemoryStream stream = null;
220 stream = new MemoryStream ();
221 Serialize (new BinaryWriter (stream));
222 return stream.GetBuffer ();
231 internal static HttpStaticObjectsCollection FromByteArray (byte [] data)
233 HttpStaticObjectsCollection objs = null;
234 MemoryStream stream = null;
236 stream = new MemoryStream (data);
237 objs = Deserialize (new BinaryReader (stream));