SynchronizedCollection.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 SynchronizedCollection<T> : IList<T>, IList
  12. {
  13. List<T> items;
  14. object sync;
  15. public SynchronizedCollection()
  16. {
  17. this.items = new List<T>();
  18. this.sync = new Object();
  19. }
  20. public SynchronizedCollection(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 SynchronizedCollection(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 SynchronizedCollection(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. public int Count
  48. {
  49. get { lock (this.sync) { return this.items.Count; } }
  50. }
  51. protected List<T> Items
  52. {
  53. get { return this.items; }
  54. }
  55. public object SyncRoot
  56. {
  57. get { return this.sync; }
  58. }
  59. public T this[int index]
  60. {
  61. get
  62. {
  63. lock (this.sync)
  64. {
  65. return this.items[index];
  66. }
  67. }
  68. set
  69. {
  70. lock (this.sync)
  71. {
  72. if (index < 0 || index >= this.items.Count)
  73. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("index", index,
  74. SR.GetString(SR.ValueMustBeInRange, 0, this.items.Count - 1)));
  75. this.SetItem(index, value);
  76. }
  77. }
  78. }
  79. public void Add(T item)
  80. {
  81. lock (this.sync)
  82. {
  83. int index = this.items.Count;
  84. this.InsertItem(index, item);
  85. }
  86. }
  87. public void Clear()
  88. {
  89. lock (this.sync)
  90. {
  91. this.ClearItems();
  92. }
  93. }
  94. public void CopyTo(T[] array, int index)
  95. {
  96. lock (this.sync)
  97. {
  98. this.items.CopyTo(array, index);
  99. }
  100. }
  101. public bool Contains(T item)
  102. {
  103. lock (this.sync)
  104. {
  105. return this.items.Contains(item);
  106. }
  107. }
  108. public IEnumerator<T> GetEnumerator()
  109. {
  110. lock (this.sync)
  111. {
  112. return this.items.GetEnumerator();
  113. }
  114. }
  115. public int IndexOf(T item)
  116. {
  117. lock (this.sync)
  118. {
  119. return this.InternalIndexOf(item);
  120. }
  121. }
  122. public void Insert(int index, T item)
  123. {
  124. lock (this.sync)
  125. {
  126. if (index < 0 || index > this.items.Count)
  127. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("index", index,
  128. SR.GetString(SR.ValueMustBeInRange, 0, this.items.Count)));
  129. this.InsertItem(index, item);
  130. }
  131. }
  132. int InternalIndexOf(T item)
  133. {
  134. int count = items.Count;
  135. for (int i = 0; i < count; i++)
  136. {
  137. if (object.Equals(items[i], item))
  138. {
  139. return i;
  140. }
  141. }
  142. return -1;
  143. }
  144. public bool Remove(T item)
  145. {
  146. lock (this.sync)
  147. {
  148. int index = this.InternalIndexOf(item);
  149. if (index < 0)
  150. return false;
  151. this.RemoveItem(index);
  152. return true;
  153. }
  154. }
  155. public void RemoveAt(int index)
  156. {
  157. lock (this.sync)
  158. {
  159. if (index < 0 || index >= this.items.Count)
  160. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("index", index,
  161. SR.GetString(SR.ValueMustBeInRange, 0, this.items.Count - 1)));
  162. this.RemoveItem(index);
  163. }
  164. }
  165. protected virtual void ClearItems()
  166. {
  167. this.items.Clear();
  168. }
  169. protected virtual void InsertItem(int index, T item)
  170. {
  171. this.items.Insert(index, item);
  172. }
  173. protected virtual void RemoveItem(int index)
  174. {
  175. this.items.RemoveAt(index);
  176. }
  177. protected virtual void SetItem(int index, T item)
  178. {
  179. this.items[index] = item;
  180. }
  181. bool ICollection<T>.IsReadOnly
  182. {
  183. get { return false; }
  184. }
  185. IEnumerator IEnumerable.GetEnumerator()
  186. {
  187. return ((IList)this.items).GetEnumerator();
  188. }
  189. bool ICollection.IsSynchronized
  190. {
  191. get { return true; }
  192. }
  193. object ICollection.SyncRoot
  194. {
  195. get { return this.sync; }
  196. }
  197. void ICollection.CopyTo(Array array, int index)
  198. {
  199. lock (this.sync)
  200. {
  201. ((IList)this.items).CopyTo(array, index);
  202. }
  203. }
  204. object IList.this[int index]
  205. {
  206. get
  207. {
  208. return this[index];
  209. }
  210. set
  211. {
  212. VerifyValueType(value);
  213. this[index] = (T)value;
  214. }
  215. }
  216. bool IList.IsReadOnly
  217. {
  218. get { return false; }
  219. }
  220. bool IList.IsFixedSize
  221. {
  222. get { return false; }
  223. }
  224. int IList.Add(object value)
  225. {
  226. VerifyValueType(value);
  227. lock (this.sync)
  228. {
  229. this.Add((T)value);
  230. return this.Count - 1;
  231. }
  232. }
  233. bool IList.Contains(object value)
  234. {
  235. VerifyValueType(value);
  236. return this.Contains((T)value);
  237. }
  238. int IList.IndexOf(object value)
  239. {
  240. VerifyValueType(value);
  241. return this.IndexOf((T)value);
  242. }
  243. void IList.Insert(int index, object value)
  244. {
  245. VerifyValueType(value);
  246. this.Insert(index, (T)value);
  247. }
  248. void IList.Remove(object value)
  249. {
  250. VerifyValueType(value);
  251. this.Remove((T)value);
  252. }
  253. static void VerifyValueType(object value)
  254. {
  255. if (value == null)
  256. {
  257. if (typeof(T).IsValueType)
  258. {
  259. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.SynchronizedCollectionWrongTypeNull)));
  260. }
  261. }
  262. else if (!(value is T))
  263. {
  264. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.SynchronizedCollectionWrongType1, value.GetType().FullName)));
  265. }
  266. }
  267. }
  268. }