|
|
@@ -11,54 +11,54 @@ public class ConcurrentList<T> : IList<T>, IList, IEnumerable, IEnumerable<T> {
|
|
|
public ConcurrentList() { }
|
|
|
public ConcurrentList(IEnumerable<T> data) => _items = new(data);
|
|
|
public static implicit operator ConcurrentList<T>(List<T> data) => new(data);
|
|
|
- public static implicit operator List<T>(ConcurrentList<T> data) { lock (data._syncLock) { return new(data._items); } }
|
|
|
+ public static implicit operator List<T>(ConcurrentList<T> data) { lock (data.SyncRoot) { return new(data._items); } }
|
|
|
|
|
|
// 枚举器
|
|
|
/// <summary>
|
|
|
/// 返回此列表的副本。
|
|
|
/// </summary>
|
|
|
- public IEnumerator<T> GetEnumerator() { lock (_syncLock) return _items.ToList().GetEnumerator(); }
|
|
|
+ public IEnumerator<T> GetEnumerator() { lock (SyncRoot) return _items.ToList().GetEnumerator(); }
|
|
|
/// <summary>
|
|
|
/// 返回此列表的副本。
|
|
|
/// </summary>
|
|
|
- IEnumerator IEnumerable.GetEnumerator() { lock (_syncLock) return _items.ToList().GetEnumerator(); }
|
|
|
+ IEnumerator IEnumerable.GetEnumerator() { lock (SyncRoot) return _items.ToList().GetEnumerator(); }
|
|
|
|
|
|
// 成员
|
|
|
- public int Count { get { lock (_syncLock) { return _items.Count; } } }
|
|
|
+ public int Count { get { lock (SyncRoot) { return _items.Count; } } }
|
|
|
public bool IsReadOnly => false;
|
|
|
bool IList.IsFixedSize => false;
|
|
|
bool ICollection.IsSynchronized => true;
|
|
|
- private readonly object _syncLock = new();
|
|
|
- object ICollection.SyncRoot => _syncLock;
|
|
|
+ public readonly object SyncRoot = new();
|
|
|
+ object ICollection.SyncRoot => SyncRoot;
|
|
|
|
|
|
public T this[int index] {
|
|
|
- get { lock (_syncLock) { return _items[index]; } }
|
|
|
- set { lock (_syncLock) { _items[index] = value; } }
|
|
|
+ get { lock (SyncRoot) { return _items[index]; } }
|
|
|
+ set { lock (SyncRoot) { _items[index] = value; } }
|
|
|
}
|
|
|
object? IList.this[int index] {
|
|
|
get => this[index];
|
|
|
- set { lock (_syncLock) { _items[index] = (T) value!; } }
|
|
|
+ set { lock (SyncRoot) { _items[index] = (T) value!; } }
|
|
|
}
|
|
|
|
|
|
- public void Add(T item) { lock (_syncLock) { _items.Add(item); } }
|
|
|
- int IList.Add(object? value) { lock (_syncLock) { _items.Add((T) value!); return _items.Count - 1; } }
|
|
|
+ public void Add(T item) { lock (SyncRoot) { _items.Add(item); } }
|
|
|
+ int IList.Add(object? value) { lock (SyncRoot) { _items.Add((T) value!); return _items.Count - 1; } }
|
|
|
|
|
|
- public void Insert(int index, T item) { lock (_syncLock) { _items.Insert(index, item); } }
|
|
|
- void IList.Insert(int index, object? value) { lock (_syncLock) { _items.Insert(index, (T) value!); } }
|
|
|
+ public void Insert(int index, T item) { lock (SyncRoot) { _items.Insert(index, item); } }
|
|
|
+ void IList.Insert(int index, object? value) { lock (SyncRoot) { _items.Insert(index, (T) value!); } }
|
|
|
|
|
|
- public bool Remove(T item) { lock (_syncLock) { return _items.Remove(item); } }
|
|
|
- void IList.Remove(object? value) { lock (_syncLock) { if (value is T t) _items.Remove(t); } }
|
|
|
+ public bool Remove(T item) { lock (SyncRoot) { return _items.Remove(item); } }
|
|
|
+ void IList.Remove(object? value) { lock (SyncRoot) { if (value is T t) _items.Remove(t); } }
|
|
|
|
|
|
- public void RemoveAt(int index) { lock (_syncLock) { _items.RemoveAt(index); } }
|
|
|
- public void Clear() { lock (_syncLock) { _items.Clear(); } }
|
|
|
+ public void RemoveAt(int index) { lock (SyncRoot) { _items.RemoveAt(index); } }
|
|
|
+ public void Clear() { lock (SyncRoot) { _items.Clear(); } }
|
|
|
|
|
|
- public bool Contains(T item) { lock (_syncLock) { return _items.Contains(item); } }
|
|
|
- bool IList.Contains(object? value) { lock (_syncLock) { return value is T t && _items.Contains(t); } }
|
|
|
+ public bool Contains(T item) { lock (SyncRoot) { return _items.Contains(item); } }
|
|
|
+ bool IList.Contains(object? value) { lock (SyncRoot) { return value is T t && _items.Contains(t); } }
|
|
|
|
|
|
- public int IndexOf(T item) { lock (_syncLock) { return _items.IndexOf(item); } }
|
|
|
- int IList.IndexOf(object? value) { lock (_syncLock) { return value is T t ? _items.IndexOf(t) : -1; } }
|
|
|
+ public int IndexOf(T item) { lock (SyncRoot) { return _items.IndexOf(item); } }
|
|
|
+ int IList.IndexOf(object? value) { lock (SyncRoot) { return value is T t ? _items.IndexOf(t) : -1; } }
|
|
|
|
|
|
- public void CopyTo(T[] array, int arrayIndex) { lock (_syncLock) { _items.CopyTo(array, arrayIndex); } }
|
|
|
- void ICollection.CopyTo(Array array, int index) { lock (_syncLock) { ((ICollection) _items).CopyTo(array, index); } }
|
|
|
+ public void CopyTo(T[] array, int arrayIndex) { lock (SyncRoot) { _items.CopyTo(array, arrayIndex); } }
|
|
|
+ void ICollection.CopyTo(Array array, int index) { lock (SyncRoot) { ((ICollection) _items).CopyTo(array, index); } }
|
|
|
|
|
|
}
|