mscorlib(4.0.0.0) API with additions
ReadOnlyCollection.cs
2 using System.Diagnostics;
4 using System.Threading;
5 
7 {
10  [Serializable]
11  [ComVisible(false)]
12  [DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))]
13  [DebuggerDisplay("Count = {Count}")]
14  [__DynamicallyInvokable]
16  {
17  private IList<T> list;
18 
19  [NonSerialized]
20  private object _syncRoot;
21 
24  [__DynamicallyInvokable]
25  public int Count
26  {
27  [__DynamicallyInvokable]
28  get
29  {
30  return list.Count;
31  }
32  }
33 
40  [__DynamicallyInvokable]
41  public T this[int index]
42  {
43  [__DynamicallyInvokable]
44  get
45  {
46  return list[index];
47  }
48  }
49 
52  [__DynamicallyInvokable]
53  protected IList<T> Items
54  {
55  [__DynamicallyInvokable]
56  get
57  {
58  return list;
59  }
60  }
61 
65  [__DynamicallyInvokable]
66  bool ICollection<T>.IsReadOnly
67  {
68  [__DynamicallyInvokable]
69  get
70  {
71  return true;
72  }
73  }
74 
79  [__DynamicallyInvokable]
80  T IList<T>.this[int index]
81  {
82  [__DynamicallyInvokable]
83  get
84  {
85  return list[index];
86  }
87  [__DynamicallyInvokable]
88  set
89  {
90  ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
91  }
92  }
93 
97  [__DynamicallyInvokable]
98  bool ICollection.IsSynchronized
99  {
100  [__DynamicallyInvokable]
101  get
102  {
103  return false;
104  }
105  }
106 
109  [__DynamicallyInvokable]
110  object ICollection.SyncRoot
111  {
112  [__DynamicallyInvokable]
113  get
114  {
115  if (_syncRoot == null)
116  {
117  ICollection collection = list as ICollection;
118  if (collection != null)
119  {
120  _syncRoot = collection.SyncRoot;
121  }
122  else
123  {
124  Interlocked.CompareExchange<object>(ref _syncRoot, new object(), (object)null);
125  }
126  }
127  return _syncRoot;
128  }
129  }
130 
134  [__DynamicallyInvokable]
135  bool IList.IsFixedSize
136  {
137  [__DynamicallyInvokable]
138  get
139  {
140  return true;
141  }
142  }
143 
147  [__DynamicallyInvokable]
148  bool IList.IsReadOnly
149  {
150  [__DynamicallyInvokable]
151  get
152  {
153  return true;
154  }
155  }
156 
163  [__DynamicallyInvokable]
164  object IList.this[int index]
165  {
166  [__DynamicallyInvokable]
167  get
168  {
169  return list[index];
170  }
171  [__DynamicallyInvokable]
172  set
173  {
174  ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
175  }
176  }
177 
182  [__DynamicallyInvokable]
184  {
185  if (list == null)
186  {
187  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.list);
188  }
189  this.list = list;
190  }
191 
196  [__DynamicallyInvokable]
197  public bool Contains(T value)
198  {
199  return list.Contains(value);
200  }
201 
210  [__DynamicallyInvokable]
211  public void CopyTo(T[] array, int index)
212  {
213  list.CopyTo(array, index);
214  }
215 
218  [__DynamicallyInvokable]
220  {
221  return list.GetEnumerator();
222  }
223 
227  [__DynamicallyInvokable]
228  public int IndexOf(T value)
229  {
230  return list.IndexOf(value);
231  }
232 
236  [__DynamicallyInvokable]
237  void ICollection<T>.Add(T value)
238  {
239  ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
240  }
241 
244  [__DynamicallyInvokable]
245  void ICollection<T>.Clear()
246  {
247  ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
248  }
249 
254  [__DynamicallyInvokable]
255  void IList<T>.Insert(int index, T value)
256  {
257  ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
258  }
259 
265  [__DynamicallyInvokable]
266  bool ICollection<T>.Remove(T value)
267  {
268  ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
269  return false;
270  }
271 
275  [__DynamicallyInvokable]
276  void IList<T>.RemoveAt(int index)
277  {
278  ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
279  }
280 
283  [__DynamicallyInvokable]
285  {
286  return ((IEnumerable)list).GetEnumerator();
287  }
288 
299  [__DynamicallyInvokable]
300  void ICollection.CopyTo(Array array, int index)
301  {
302  if (array == null)
303  {
304  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
305  }
306  if (array.Rank != 1)
307  {
308  ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
309  }
310  if (array.GetLowerBound(0) != 0)
311  {
312  ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound);
313  }
314  if (index < 0)
315  {
316  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.arrayIndex, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
317  }
318  if (array.Length - index < Count)
319  {
320  ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
321  }
322  T[] array2 = array as T[];
323  if (array2 != null)
324  {
325  list.CopyTo(array2, index);
326  return;
327  }
328  Type elementType = array.GetType().GetElementType();
329  Type typeFromHandle = typeof(T);
330  if (!elementType.IsAssignableFrom(typeFromHandle) && !typeFromHandle.IsAssignableFrom(elementType))
331  {
332  ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);
333  }
334  object[] array3 = array as object[];
335  if (array3 == null)
336  {
337  ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);
338  }
339  int count = list.Count;
340  try
341  {
342  for (int i = 0; i < count; i++)
343  {
344  array3[index++] = list[i];
345  }
346  }
347  catch (ArrayTypeMismatchException)
348  {
349  ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);
350  }
351  }
352 
357  [__DynamicallyInvokable]
358  int IList.Add(object value)
359  {
360  ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
361  return -1;
362  }
363 
366  [__DynamicallyInvokable]
367  void IList.Clear()
368  {
369  ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
370  }
371 
372  private static bool IsCompatibleObject(object value)
373  {
374  if (!(value is T))
375  {
376  if (value == null)
377  {
378  return default(T) == null;
379  }
380  return false;
381  }
382  return true;
383  }
384 
391  [__DynamicallyInvokable]
392  bool IList.Contains(object value)
393  {
394  if (IsCompatibleObject(value))
395  {
396  return Contains((T)value);
397  }
398  return false;
399  }
400 
406  [__DynamicallyInvokable]
407  int IList.IndexOf(object value)
408  {
409  if (IsCompatibleObject(value))
410  {
411  return IndexOf((T)value);
412  }
413  return -1;
414  }
415 
420  [__DynamicallyInvokable]
421  void IList.Insert(int index, object value)
422  {
423  ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
424  }
425 
429  [__DynamicallyInvokable]
430  void IList.Remove(object value)
431  {
432  ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
433  }
434 
438  [__DynamicallyInvokable]
439  void IList.RemoveAt(int index)
440  {
441  ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
442  }
443  }
444 }
int IndexOf(T value)
Searches for the specified object and returns the zero-based index of the first occurrence within the...
Provides the base class for a generic read-only collection.
Represents a non-generic collection of objects that can be individually accessed by index.
Definition: IList.cs:8
Definition: __Canon.cs:3
new IEnumerator< T > GetEnumerator()
Returns an enumerator that iterates through the collection.
bool Remove(T item)
Removes the first occurrence of a specific object from the T:System.Collections.Generic....
int Count
Gets the number of elements contained in the T:System.Collections.ObjectModel.ReadOnlyCollection`1 in...
Exposes the enumerator, which supports a simple iteration over a collection of a specified type....
Definition: IEnumerable.cs:9
void RemoveAt(int index)
Removes the T:System.Collections.Generic.IList`1 item at the specified index.
Represents a strongly-typed, read-only collection of elements.
Represents a read-only collection of elements that can be accessed by index.
Definition: IReadOnlyList.cs:9
void CopyTo(T[] array, int arrayIndex)
Copies the elements of the T:System.Collections.Generic.ICollection`1 to an T:System....
void Insert(int index, T item)
Inserts an item to the T:System.Collections.Generic.IList`1 at the specified index.
Defines methods to manipulate generic collections.
Definition: ICollection.cs:9
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
bool Contains(T item)
Determines whether the T:System.Collections.Generic.ICollection`1 contains a specific value.
static int CompareExchange(ref int location1, int value, int comparand)
Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value.
bool IsReadOnly
Gets a value indicating whether the T:System.Collections.Generic.ICollection`1 is read-only.
Definition: ICollection.cs:25
IList< T > Items
Returns the T:System.Collections.Generic.IList`1 that the T:System.Collections.ObjectModel....
int IndexOf(T item)
Determines the index of a specific item in the T:System.Collections.Generic.IList`1.
Represents a collection of objects that can be individually accessed by index.
Definition: IList.cs:9
ReadOnlyCollection(IList< T > list)
Initializes a new instance of the T:System.Collections.ObjectModel.ReadOnlyCollection`1 class that is...
IEnumerator< T > GetEnumerator()
Returns an enumerator that iterates through the T:System.Collections.ObjectModel.ReadOnlyCollection`1...
bool Contains(T value)
Determines whether an element is in the T:System.Collections.ObjectModel.ReadOnlyCollection`1.
void CopyTo(T[] array, int index)
Copies the entire T:System.Collections.ObjectModel.ReadOnlyCollection`1 to a compatible one-dimension...
Specifies that the class can be serialized.
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition: ICollection.cs:8
Provides atomic operations for variables that are shared by multiple threads.
Definition: Interlocked.cs:10
void Add(T item)
Adds an item to the T:System.Collections.Generic.ICollection`1.
void Clear()
Removes all items from the T:System.Collections.Generic.ICollection`1.