using System; using System.Collections.Generic; using System.Threading; namespace Preparation.Utility { public class LockedClassList where T : class { private readonly ReaderWriterLockSlim listLock = new(); private List list; #region 构造 public LockedList() { list = new List(); } public LockedList(int capacity) { list = new List(capacity); } public LockedList(IEnumerable collection) { list = new List(collection); } #endregion #region 修改 public TResult WriteLock(Func func) { listLock.EnterWriteLock(); try { return func(); } finally { listLock.ExitWriteLock(); } } public void WriteLock(Action func) { listLock.EnterWriteLock(); try { func(); } finally { listLock.ExitWriteLock(); } } public void Add(T item) { WriteLock(() => { list.Add(item); }); } public void Insert(int index, T item) { WriteLock(() => { list.Insert(index, item); }); } public void Clear() { WriteLock(() => { list.Clear(); }); } public bool Remove(T item) { return WriteLock(() => { return list.Remove(item); }); } public int RemoveAll(T item) => WriteLock(() => { return list.RemoveAll((t) => { return t == item; }); }); public bool RemoveOne(Predicate match) => WriteLock(() => { int index = list.FindIndex(match); if (index == -1) return false; list.RemoveAt(index); return true; }); public int RemoveAll(Predicate match) => WriteLock(() => { return list.RemoveAll(match); }); public bool RemoveAt(int index) { return WriteLock(() => { if (index > list.Count) return false; list.RemoveAt(index); return true; }); } #endregion #region 读取与对类操作 public TResult ReadLock(Func func) { listLock.EnterReadLock(); try { return func(); } finally { listLock.ExitReadLock(); } } public void ReadLock(Action func) { listLock.EnterReadLock(); try { func(); } finally { listLock.ExitReadLock(); } } public T this[int index] { get { return ReadLock(() => { return list[index]; }); } set { ReadLock(() => { list[index] = value!; }); } } public int Count => ReadLock(() => { return list.Count; }); public int IndexOf(T item) { return ReadLock(() => { return list.IndexOf(item); }); } public bool Contains(T item) { return ReadLock(() => { return list.Contains(item); }); } public T? Find(Predicate match) { return ReadLock(() => { return list.Find(match); }); } public List FindAll(Predicate match) { return ReadLock(() => { return list.FindAll(match); }); } public int FindIndex(Predicate match) => ReadLock(() => { return list.FindIndex(match); }); public void ForEach(Action action) => ReadLock(() => { list.ForEach(action); }); #endregion } }