Debian Codegraph @

mcs/class/referencesource/mscorlib/system/collections/objectmodel/readonlycollection.cs

raw
1 // ==++==
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // ==--==
6 // <OWNER>Microsoft</OWNER>
7 //
8
9 namespace System.Collections.ObjectModel
10 {
11 using System;
12 using System.Collections;
13 using System.Collections.Generic;
14 using System.Diagnostics;
15 using System.Runtime;
16
17 [Serializable]
18 [System.Runtime.InteropServices.ComVisible(false)]
19 [DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))]
20 [DebuggerDisplay("Count = {Count}")]
21 public class ReadOnlyCollection<T>: IList<T>, IList, IReadOnlyList<T>
22 {
23 IList<T> list;
24 [NonSerialized]
25 private Object _syncRoot;
26
27 public ReadOnlyCollection(IList<T> list) {
28 if (list == null) {
29 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.list);
30 }
31 this.list = list;
32 }
33
34 public int Count {
35 get { return list.Count; }
36 }
37
38 public T this[int index] {
39 get { return list[index]; }
40 }
41
42 public bool Contains(T value) {
43 return list.Contains(value);
44 }
45
46 public void CopyTo(T[] array, int index) {
47 list.CopyTo(array, index);
48 }
49
50 public IEnumerator<T> GetEnumerator() {
51 return list.GetEnumerator();
52 }
53
54 public int IndexOf(T value) {
55 return list.IndexOf(value);
56 }
57
58 protected IList<T> Items {
59 get {
60 return list;
61 }
62 }
63
64 bool ICollection<T>.IsReadOnly {
65 get { return true; }
66 }
67
68 T IList<T>.this[int index] {
69 get { return list[index]; }
70 set {
71 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
72 }
73 }
74
75 void ICollection<T>.Add(T value) {
76 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
77 }
78
79 void ICollection<T>.Clear() {
80 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
81 }
82
83 void IList<T>.Insert(int index, T value) {
84 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
85 }
86
87 bool ICollection<T>.Remove(T value) {
88 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
89 return false;
90 }
91
92 void IList<T>.RemoveAt(int index) {
93 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
94 }
95
96 IEnumerator IEnumerable.GetEnumerator() {
97 return ((IEnumerable)list).GetEnumerator();
98 }
99
100 bool ICollection.IsSynchronized {
101 get { return false; }
102 }
103
104 object ICollection.SyncRoot {
105 get {
106 if( _syncRoot == null) {
107 ICollection c = list as ICollection;
108 if( c != null) {
109 _syncRoot = c.SyncRoot;
110 }
111 else {
112 System.Threading.Interlocked.CompareExchange<Object>(ref _syncRoot, new Object(), null);
113 }
114 }
115 return _syncRoot;
116 }
117 }
118
119 void ICollection.CopyTo(Array array, int index) {
120 if (array==null) {
121 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
122 }
123
124 if (array.Rank != 1) {
125 ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
126 }
127
128 if( array.GetLowerBound(0) != 0 ) {
129 ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound);
130 }
131
132 if (index < 0) {
133 ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.arrayIndex, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
134 }
135
136 if (array.Length - index < Count) {
137 ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
138 }
139
140 T[] items = array as T[];
141 if (items != null) {
142 list.CopyTo(items, index);
143 }
144 else {
145 //
146 // Catch the obvious case assignment will fail.
147 // We can found all possible problems by doing the check though.
148 // For example, if the element type of the Array is derived from T,
149 // we can't figure out if we can successfully copy the element beforehand.
150 //
151 Type targetType = array.GetType().GetElementType();
152 Type sourceType = typeof(T);
153 if(!(targetType.IsAssignableFrom(sourceType) || sourceType.IsAssignableFrom(targetType))) {
154 ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);
155 }
156
157 //
158 // We can't cast array of value type to object[], so we don't support
159 // widening of primitive types here.
160 //
161 object[] objects = array as object[];
162 if( objects == null) {
163 ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);
164 }
165
166 int count = list.Count;
167 try {
168 for (int i = 0; i < count; i++) {
169 objects[index++] = list[i];
170 }
171 }
172 catch(ArrayTypeMismatchException) {
173 ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);
174 }
175 }
176 }
177
178 bool IList.IsFixedSize {
179 get { return true; }
180 }
181
182 bool IList.IsReadOnly {
183 get { return true; }
184 }
185
186 object IList.this[int index] {
187 get { return list[index]; }
188 set {
189 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
190 }
191 }
192
193 int IList.Add(object value) {
194 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
195 return -1;
196 }
197
198 void IList.Clear() {
199 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
200 }
201
202 private static bool IsCompatibleObject(object value) {
203 // Non-null values are fine. Only accept nulls if T is a class or Nullable<U>.
204 // Note that default(T) is not equal to null for value types except when T is Nullable<U>.
205 return ((value is T) || (value == null && default(T) == null));
206 }
207
208 bool IList.Contains(object value) {
209 if(IsCompatibleObject(value)) {
210 return Contains((T) value);
211 }
212 return false;
213 }
214
215 int IList.IndexOf(object value) {
216 if(IsCompatibleObject(value)) {
217 return IndexOf((T)value);
218 }
219 return -1;
220 }
221
222 void IList.Insert(int index, object value) {
223 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
224 }
225
226 void IList.Remove(object value) {
227 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
228 }
229
230 void IList.RemoveAt(int index) {
231 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
232 }
233 }
234 }