using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LB_VisionProcesses { [Serializable] public class ProcessCollections : ICollection> { private readonly object _lock = new object(); // 线程安全锁 private List> _items = new List>(); // 通过名称查找 public ProcessCollectionItem Get(string name) { lock (_lock) { var item = _items.FirstOrDefault(item => item.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); if (item == null) { //// 如果不存在,创建新项并分配索引 //_items.Add(item); return default; } return item; } } // 通过索引查找 public ProcessCollectionItem Get(int index) { lock (_lock) { var item = _items.FirstOrDefault(item => item.Index == index); if (item == null) { // 如果不存在,创建新项并分配索引 _items.Add(item); return default; } return item; } } public Dictionary GetAllByName() { try { Dictionary dic = new Dictionary(); for (int i = 0; i <= _items.Count - 1; i++) dic.TryAdd(_items[i].Name, _items[i].Value); return dic; } catch { return new Dictionary(); } } public List GetAllByIndex() { try { List list = new List(); for (int i = 0; i <= _items.Count - 1; i++) list.Add(_items[i].Value); return list; } catch { return new List(); } } // ICollection> 实现 public int Count => _items.Count; public bool IsReadOnly => false; void ICollection>.Add(ProcessCollectionItem item) { lock (_lock) { var existingItem = Get(item.Name); if (existingItem != null) { // 如果已存在,更新现有项的值 existingItem = item; } else { // 如果不存在,创建新项并分配索引 _items.Add(item); } // 更新后自动排序 SortItems(); } } // 添加项时为新项分配一个索引 public void Add(string name, T value) { lock (_lock) { var existingItem = Get(name); if (existingItem != null) { // 如果已存在,更新现有项的值 existingItem.Value = value; } else { // 如果不存在,创建新项并分配索引 var newItem = new ProcessCollectionItem(name, _items.Count > 0 ? _items.Max(i => i.Index) + 1 : 0, value); _items.Add(newItem); } // 更新后自动排序 SortItems(); } } // 按 Index 排序集合 private void SortItems() { lock (_lock) { _items = _items.OrderBy(item => item.Index).ToList(); } } // 通过索引器实现基于序号的访问 public T this[int index] { get { if (index < 0 || index >= _items.Count) { return default; } return _items[index].Value; } set { lock (_lock) { if (index < 0 || index >= _items.Count) { return; } _items[index].Value = value; } } } public T this[string name] { get { var existingItem = Get(name); if (existingItem != null) { // 如果已存在,更新现有项的值 return existingItem.Value; } return default; } set { lock (_lock) { var item = _items.FirstOrDefault(i => i.Name == name); if (item != null) { item.Value = value; } } } } public void Clear() { lock (_lock) { _items.Clear(); _items.TrimExcess(); // 将容量缩减到实际元素数量 } } public bool Contains(ProcessCollectionItem item) { return _items.Contains(item); } public bool Contains(string name) { var item = _items.FirstOrDefault(i => i.Name == name); if (item != null) return true; return false; } public bool ContainsKey(string name) { var item = _items.FirstOrDefault(i => i.Name == name); if (item != null) return true; return false; } public bool GetKey(int index, out string name) { if (index < 0 || index >= _items.Count) { name = null; return false; } name = _items[index].Name; return true; } public void CopyTo(ProcessCollectionItem[] array, int arrayIndex) { lock (_lock) { _items.CopyTo(array, arrayIndex); } } public bool Remove(ProcessCollectionItem item) { lock (_lock) { var removed = _items.Remove(item); if (removed) { // 移除元素后,调整后续元素的索引 UpdateIndexes(); } return removed; } } public IEnumerator> GetEnumerator() { lock (_lock) { return _items.GetEnumerator(); } } IEnumerator IEnumerable.GetEnumerator() { lock (_lock) { return _items.GetEnumerator(); } } // 通过名称删除 public bool Remove(string name) { lock (_lock) { var namedItem = Get(name); if (namedItem != null) { return Remove(namedItem); } return false; } } // 通过索引删除 public bool Remove(int index) { lock (_lock) { var namedItem = Get(index); if (namedItem != null) { return Remove(namedItem); } return false; } } // 删除项后,调整其他项的索引 private void UpdateIndexes() { lock (_lock) { for (int i = 0; i < _items.Count; i++) { _items[i].Index = i; } } } } public class ProcessCollectionItem { public string Name { get; set; } public int Index { get; set; } public T Value { get; set; } // 存储的对象 public ProcessCollectionItem(string name, int index, T value) { Name = name; Index = index; Value = value; } public override string ToString() { return $"{Name} (Index: {Index}, Value: {Value})"; } } }