LockedList.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. namespace Quadarax.Foundation.Core.Collections
  4. {
  5. public class LockedList<TElement> : IList<TElement>
  6. {
  7. #region *** Private Fields ***
  8. private readonly List<TElement> _list;
  9. #endregion
  10. #region *** Public Properties ***
  11. public int Count
  12. {
  13. get
  14. {
  15. lock (_list)
  16. {
  17. return _list.Count;
  18. }
  19. }
  20. }
  21. public bool IsReadOnly
  22. {
  23. get
  24. {
  25. lock (_list)
  26. {
  27. return ((IList<TElement>)_list).IsReadOnly;
  28. }
  29. }
  30. }
  31. #endregion
  32. #region *** Indexers ***
  33. public TElement this[int index]
  34. {
  35. get
  36. {
  37. lock (_list)
  38. {
  39. return _list[index];
  40. }
  41. }
  42. set
  43. {
  44. lock (_list)
  45. {
  46. _list[index] = value;
  47. }
  48. }
  49. }
  50. #endregion
  51. #region *** Constructors ***
  52. public LockedList()
  53. {
  54. _list = new List<TElement>();
  55. }
  56. public LockedList(IEnumerable<TElement> initialItems)
  57. {
  58. _list = new List<TElement>(initialItems);
  59. }
  60. public LockedList(int capacity)
  61. {
  62. _list = new List<TElement>(capacity);
  63. }
  64. #endregion
  65. #region *** Public Methods ***
  66. public IEnumerator<TElement> GetEnumerator()
  67. {
  68. lock (_list)
  69. {
  70. return _list.GetEnumerator();
  71. }
  72. }
  73. IEnumerator IEnumerable.GetEnumerator()
  74. {
  75. return GetEnumerator();
  76. }
  77. public void Add(TElement item)
  78. {
  79. lock (_list)
  80. {
  81. _list.Add(item);
  82. }
  83. }
  84. public void Clear()
  85. {
  86. lock (_list)
  87. {
  88. _list.Clear();
  89. }
  90. }
  91. public bool Contains(TElement item)
  92. {
  93. lock (_list)
  94. {
  95. return _list.Contains(item);
  96. }
  97. }
  98. public void CopyTo(TElement[] array, int arrayIndex)
  99. {
  100. lock (_list)
  101. {
  102. _list.CopyTo(array, arrayIndex);
  103. }
  104. }
  105. public bool Remove(TElement item)
  106. {
  107. lock (_list)
  108. {
  109. return _list.Remove(item);
  110. }
  111. }
  112. public int IndexOf(TElement item)
  113. {
  114. lock (_list)
  115. {
  116. return _list.IndexOf(item);
  117. }
  118. }
  119. public void Insert(int index, TElement item)
  120. {
  121. lock (_list)
  122. {
  123. _list.Insert(index, item);
  124. }
  125. }
  126. public void RemoveAt(int index)
  127. {
  128. lock (_list)
  129. {
  130. _list.RemoveAt(index);
  131. }
  132. }
  133. #endregion
  134. }
  135. }