1 // -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 // System.Collections.ObjectModel.ReadOnlyCollection
6 // Zoltan Varga (vargaz@gmail.com)
7 // David Waite (mass@akuma.org)
9 // (C) 2005 Novell, Inc.
10 // (C) 2005 David Waite
14 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
15 // Copyright (C) 2005 David Waite
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 using System.Collections.Generic;
40 using System.Runtime.InteropServices;
42 namespace System.Collections.ObjectModel
46 public class ReadOnlyCollection <T> : IList <T>, ICollection <T>, IEnumerable <T>, IList, ICollection, IEnumerable
51 public ReadOnlyCollection (IList <T> list)
54 throw new ArgumentNullException ("list");
56 ICollection c = list as ICollection;
57 syncRoot = (c != null) ? c.SyncRoot : new object ();
60 public void Add (T item)
62 throw new NotSupportedException ();
67 throw new NotSupportedException ();
70 public bool Contains (T item)
72 return list.Contains (item);
75 public void CopyTo (T [] array, int index)
77 list.CopyTo (array, index);
80 public IEnumerator <T> GetEnumerator ()
82 return list.GetEnumerator ();
85 public int IndexOf (T item)
87 return list.IndexOf (item);
90 public void Insert (int index, T item)
92 throw new NotSupportedException ();
95 public bool Remove (T item)
97 throw new NotSupportedException ();
100 public void RemoveAt (int index)
102 throw new NotSupportedException ();
106 get { return list.Count; }
109 public T this [int index] {
110 get { return list [index]; }
111 set { throw new NotSupportedException (); }
114 public bool IsReadOnly {
118 #region Not generic interface implementations
119 void ICollection.CopyTo (Array array, int arrayIndex)
121 T [] target = array as T [];
123 throw new ArgumentException ("array");
124 list.CopyTo (target, arrayIndex);
127 IEnumerator IEnumerable.GetEnumerator ()
129 return ((IEnumerable) list).GetEnumerator ();
132 int IList.Add (object item)
134 throw new NotSupportedException ();
137 bool IList.Contains (object item)
139 if (Collection <T>.IsValidItem (item))
140 return list.Contains ((T) item);
144 int IList.IndexOf (object item)
146 if (Collection <T>.IsValidItem (item))
147 return list.IndexOf ((T) item);
151 void IList.Insert (int index, object item)
153 throw new NotSupportedException ();
156 void IList.Remove (object item)
158 throw new NotSupportedException ();
161 bool ICollection.IsSynchronized {
162 get { return Collection <T>.IsSynchronized (list); }
165 object ICollection.SyncRoot {
166 get { return syncRoot; }
169 bool IList.IsFixedSize {
170 get { return Collection <T>.IsFixedSize (list); }
173 bool IList.IsReadOnly {
177 object IList.this [int index] {
178 get { return list [index]; }
179 set { throw new NotSupportedException (); }