SynchronizedReadOnlyCollection.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.Collections.Generic
  5. {
  6. using System;
  7. using System.Collections;
  8. using System.Diagnostics;
  9. using System.ServiceModel;
  10. [System.Runtime.InteropServices.ComVisible(false)]
  11. public class SynchronizedReadOnlyCollection<T> : IList<T>, IList
  12. {
  13. IList<T> items;
  14. object sync;
  15. public SynchronizedReadOnlyCollection()
  16. {
  17. this.items = new List<T>();
  18. this.sync = new Object();
  19. }
  20. public SynchronizedReadOnlyCollection(object syncRoot)
  21. {
  22. if (syncRoot == null)
  23. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("syncRoot"));
  24. this.items = new List<T>();
  25. this.sync = syncRoot;
  26. }
  27. public SynchronizedReadOnlyCollection(object syncRoot, IEnumerable<T> list)
  28. {
  29. if (syncRoot == null)
  30. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("syncRoot"));
  31. if (list == null)
  32. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("list"));
  33. this.items = new List<T>(list);
  34. this.sync = syncRoot;
  35. }
  36. public SynchronizedReadOnlyCollection(object syncRoot, params T[] list)
  37. {
  38. if (syncRoot == null)
  39. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("syncRoot"));
  40. if (list == null)
  41. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("list"));
  42. this.items = new List<T>(list.Length);
  43. for (int i = 0; i < list.Length; i++)
  44. this.items.Add(list[i]);
  45. this.sync = syncRoot;
  46. }
  47. internal SynchronizedReadOnlyCollection(object syncRoot, List<T> list, bool makeCopy)
  48. {
  49. if (syncRoot == null)
  50. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("syncRoot"));
  51. if (list == null)
  52. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("list"));
  53. if (makeCopy)
  54. this.items = new List<T>(list);
  55. else
  56. this.items = list;
  57. this.sync = syncRoot;
  58. }
  59. public int Count
  60. {
  61. get { lock (this.sync) { return this.items.Count; } }
  62. }
  63. protected IList<T> Items
  64. {
  65. get
  66. {
  67. return this.items;
  68. }
  69. }
  70. public T this[int index]
  71. {
  72. get { lock (this.sync) { return this.items[index]; } }
  73. }
  74. public bool Contains(T value)
  75. {
  76. lock (this.sync)
  77. {
  78. return this.items.Contains(value);
  79. }
  80. }
  81. public void CopyTo(T[] array, int index)
  82. {
  83. lock (this.sync)
  84. {
  85. this.items.CopyTo(array, index);
  86. }
  87. }
  88. public IEnumerator<T> GetEnumerator()
  89. {
  90. lock (this.sync)
  91. {
  92. return this.items.GetEnumerator();
  93. }
  94. }
  95. public int IndexOf(T value)
  96. {
  97. lock (this.sync)
  98. {
  99. return this.items.IndexOf(value);
  100. }
  101. }
  102. void ThrowReadOnly()
  103. {
  104. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.SFxCollectionReadOnly)));
  105. }
  106. bool ICollection<T>.IsReadOnly
  107. {
  108. get { return true; }
  109. }
  110. T IList<T>.this[int index]
  111. {
  112. get
  113. {
  114. return this[index];
  115. }
  116. set
  117. {
  118. this.ThrowReadOnly();
  119. }
  120. }
  121. void ICollection<T>.Add(T value)
  122. {
  123. this.ThrowReadOnly();
  124. }
  125. void ICollection<T>.Clear()
  126. {
  127. this.ThrowReadOnly();
  128. }
  129. bool ICollection<T>.Remove(T value)
  130. {
  131. this.ThrowReadOnly();
  132. return false;
  133. }
  134. void IList<T>.Insert(int index, T value)
  135. {
  136. this.ThrowReadOnly();
  137. }
  138. void IList<T>.RemoveAt(int index)
  139. {
  140. this.ThrowReadOnly();
  141. }
  142. bool ICollection.IsSynchronized
  143. {
  144. get { return true; }
  145. }
  146. object ICollection.SyncRoot
  147. {
  148. get { return this.sync; }
  149. }
  150. void ICollection.CopyTo(Array array, int index)
  151. {
  152. ICollection asCollection = this.items as ICollection;
  153. if (asCollection == null)
  154. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.SFxCopyToRequiresICollection)));
  155. lock (this.sync)
  156. {
  157. asCollection.CopyTo(array, index);
  158. }
  159. }
  160. IEnumerator IEnumerable.GetEnumerator()
  161. {
  162. lock (this.sync)
  163. {
  164. IEnumerable asEnumerable = this.items as IEnumerable;
  165. if (asEnumerable != null)
  166. return asEnumerable.GetEnumerator();
  167. else
  168. return new EnumeratorAdapter(this.items);
  169. }
  170. }
  171. bool IList.IsFixedSize
  172. {
  173. get { return true; }
  174. }
  175. bool IList.IsReadOnly
  176. {
  177. get { return true; }
  178. }
  179. object IList.this[int index]
  180. {
  181. get
  182. {
  183. return this[index];
  184. }
  185. set
  186. {
  187. this.ThrowReadOnly();
  188. }
  189. }
  190. int IList.Add(object value)
  191. {
  192. this.ThrowReadOnly();
  193. return 0;
  194. }
  195. void IList.Clear()
  196. {
  197. this.ThrowReadOnly();
  198. }
  199. bool IList.Contains(object value)
  200. {
  201. VerifyValueType(value);
  202. return this.Contains((T)value);
  203. }
  204. int IList.IndexOf(object value)
  205. {
  206. VerifyValueType(value);
  207. return this.IndexOf((T)value);
  208. }
  209. void IList.Insert(int index, object value)
  210. {
  211. this.ThrowReadOnly();
  212. }
  213. void IList.Remove(object value)
  214. {
  215. this.ThrowReadOnly();
  216. }
  217. void IList.RemoveAt(int index)
  218. {
  219. this.ThrowReadOnly();
  220. }
  221. static void VerifyValueType(object value)
  222. {
  223. if ((value is T) || (value == null && !typeof(T).IsValueType))
  224. return;
  225. Type type = (value == null) ? typeof(Object) : value.GetType();
  226. string message = SR.GetString(SR.SFxCollectionWrongType2, type.ToString(), typeof(T).ToString());
  227. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(message));
  228. }
  229. sealed class EnumeratorAdapter : IEnumerator, IDisposable
  230. {
  231. IList<T> list;
  232. IEnumerator<T> e;
  233. public EnumeratorAdapter(IList<T> list)
  234. {
  235. this.list = list;
  236. this.e = list.GetEnumerator();
  237. }
  238. public object Current
  239. {
  240. get { return e.Current; }
  241. }
  242. public bool MoveNext()
  243. {
  244. return e.MoveNext();
  245. }
  246. public void Dispose()
  247. {
  248. e.Dispose();
  249. }
  250. public void Reset()
  251. {
  252. e = list.GetEnumerator();
  253. }
  254. }
  255. }
  256. }