2 // System.ServiceModel.SynchronizedReadOnlyCollection.cs
4 // Author: Duncan Mak (duncan@novell.com)
6 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections;
30 using System.Runtime.InteropServices;
32 namespace System.Collections.Generic
35 public class SynchronizedReadOnlyCollection<T>
36 : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
41 public SynchronizedReadOnlyCollection ()
42 : this (new object ())
46 public SynchronizedReadOnlyCollection (object syncRoot)
47 : this (syncRoot, new List<T> ())
51 public SynchronizedReadOnlyCollection (object syncRoot, IEnumerable<T> list)
54 throw new ArgumentNullException ("syncRoot");
57 throw new ArgumentNullException ("list");
59 this.sync_root = syncRoot;
60 this.l = new List<T> (list);
63 public SynchronizedReadOnlyCollection (object syncRoot, params T [] list)
64 : this (syncRoot, (IEnumerable<T>) list)
68 public SynchronizedReadOnlyCollection (object sync_root, List<T> list, bool make_copy)
70 list == null ? null : make_copy ? new List<T> (list) : list)
74 public bool Contains (T value)
79 retval = l.Contains (value);
85 public void CopyTo (T [] array, int index)
88 l.CopyTo (array, index);
92 public IEnumerator<T> GetEnumerator ()
94 IEnumerator<T> retval;
97 retval = l.GetEnumerator ();
103 public int IndexOf (T value)
108 retval = l.IndexOf (value);
114 void ICollection<T>.Add (T value) { throw new NotSupportedException (); }
115 void ICollection<T>.Clear () { throw new NotSupportedException (); }
116 bool ICollection<T>.Remove (T value) { throw new NotSupportedException (); }
118 void IList<T>.Insert (int index, T value) { throw new NotSupportedException (); }
119 void IList<T>.RemoveAt (int index) { throw new NotSupportedException (); }
121 void ICollection.CopyTo (Array array, int index)
123 ICollection<T> a = array as ICollection<T>;
126 throw new ArgumentException ("The array type is not compatible.");
129 ((ICollection) l).CopyTo (array, index);
133 IEnumerator IEnumerable.GetEnumerator ()
135 return GetEnumerator ();
138 int IList.Add (object value) { throw new NotSupportedException (); }
139 void IList.Clear () { throw new NotSupportedException (); }
141 bool IList.Contains (object value)
143 if (typeof (T).IsValueType)
144 throw new ArgumentException ("This is a collection of ValueTypes.");
146 // null always gets thru
147 if (value is T == false && value != null)
148 throw new ArgumentException ("value is not of the same type as this collection.");
153 retval = l.Contains (val);
159 int IList.IndexOf (object value)
161 if (typeof (T).IsValueType)
162 throw new ArgumentException ("This is a collection of ValueTypes.");
164 if (value is T == false)
165 throw new ArgumentException ("value is not of the same type as this collection.");
170 retval = l.IndexOf (val);
176 void IList.Insert (int index, object value) { throw new NotSupportedException (); }
177 void IList.Remove (object value) { throw new NotSupportedException (); }
178 void IList.RemoveAt (int index) { throw new NotSupportedException (); }
190 public T this [int index] {
200 protected IList<T> Items {
205 bool ICollection<T>.IsReadOnly { get { return true; }}
207 bool ICollection.IsSynchronized { get { return true; }}
208 object ICollection.SyncRoot { get { return sync_root; }}
210 bool IList.IsFixedSize { get { return true; }}
211 bool IList.IsReadOnly { get { return true; }}
213 T IList<T>.this [int index] {
214 get { return this [index]; }
215 set { throw new NotSupportedException (); }
218 object IList.this [int index] {
219 get { return this [index]; }
220 set { throw new NotSupportedException (); }