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;
27 namespace System.Web {
28 public sealed class HttpStaticObjectsCollection : ICollection, IEnumerable {
29 private Hashtable _Objects;
35 public StaticItem (Type type)
40 public StaticItem (StaticItem item)
42 this.type = item.type;
45 public object Instance {
49 instance = Activator.CreateInstance (type);
57 // Needs to hold object items that can be latebound and can be serialized
58 public HttpStaticObjectsCollection ()
60 _Objects = new Hashtable();
63 public object GetObject (string name)
68 public IEnumerator GetEnumerator ()
70 return _Objects.GetEnumerator ();
73 public void CopyTo (Array array, int index)
75 _Objects.CopyTo (array, index);
78 internal IDictionary GetObjects ()
83 public object this [string name] {
85 StaticItem item = _Objects [name] as StaticItem;
94 get { return _Objects.Count; }
97 public bool IsReadOnly {
101 public bool IsSynchronized {
102 get { return false; }
105 public object SyncRoot {
109 internal HttpStaticObjectsCollection Clone ()
111 HttpStaticObjectsCollection coll = new HttpStaticObjectsCollection ();
112 coll._Objects = new Hashtable ();
113 foreach (string key in _Objects.Keys) {
114 StaticItem item = new StaticItem ((StaticItem) _Objects [key]);
115 coll._Objects [key] = item;
121 internal void Add (ObjectTagBuilder tag)
123 _Objects.Add (tag.ObjectID, new StaticItem (tag.Type));
126 private void Set (string name, object obj)
128 _Objects [name] = obj;
131 internal void Serialize (BinaryWriter w)
135 foreach (string key in _Objects.Keys) {
137 object value = _Objects [key];
139 w.Write (System.Web.Util.AltSerialization.NullIndex);
143 System.Web.Util.AltSerialization.SerializeByType (w, value);
148 internal static HttpStaticObjectsCollection Deserialize (BinaryReader r)
150 HttpStaticObjectsCollection result = new HttpStaticObjectsCollection ();
151 for (int i = r.ReadInt32 (); i > 0; i--) {
152 string key = r.ReadString ();
153 int index = r.ReadInt32 ();
154 if (index == System.Web.Util.AltSerialization.NullIndex)
155 result.Set (key, null);
157 result.Set (key, System.Web.Util.AltSerialization.DeserializeFromIndex (index, r));
163 internal byte [] ToByteArray ()
165 MemoryStream stream = null;
167 stream = new MemoryStream ();
168 Serialize (new BinaryWriter (stream));
169 return stream.GetBuffer ();
178 internal static HttpStaticObjectsCollection FromByteArray (byte [] data)
180 HttpStaticObjectsCollection objs = null;
181 MemoryStream stream = null;
183 stream = new MemoryStream (data);
184 objs = Deserialize (new BinaryReader (stream));