using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml.Linq; namespace LB_VisionProcesses.Communicators { [Serializable] public class CommunicatorCollections : ICollection> { private List> _items = new List>(); // 通过名称查找 public CommunicatorCollectionItem Get(string name) { var item = _items.FirstOrDefault(item => item.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); if (item == null) return default; return item; } // 通过索引查找 public CommunicatorCollectionItem Get(int index) { var item = _items.FirstOrDefault(item => item.Index == index); if (item == null) return default; return item; } // ICollection> 实现 public int Count => _items.Count; public bool IsReadOnly => false; void ICollection>.Add(CommunicatorCollectionItem item) { var existingItem = Get(item.Name); if (existingItem != null) { // 如果已存在,更新现有项的值 existingItem = item; } else { // 如果不存在,创建新项并分配索引 _items.Add(item); } // 更新后自动排序 SortItems(); } // 添加项时为新项分配一个索引 public void Add(string name, T value) { var existingItem = Get(name); if (existingItem != null) { // 如果已存在,更新现有项的值 existingItem.Value = value; } else { // 如果不存在,创建新项并分配索引 var newItem = new CommunicatorCollectionItem(name, _items.Count > 0 ? _items.Max(i => i.Index) + 1 : 0, value); _items.Add(newItem); } // 更新后自动排序 SortItems(); } // 按 Index 排序集合 private void SortItems() { _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 { 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 { var item = _items.FirstOrDefault(i => i.Name == name); if (item != null) { item.Value = value; } } } public void Clear() { _items.Clear(); } public bool Contains(CommunicatorCollectionItem 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 void CopyTo(CommunicatorCollectionItem[] array, int arrayIndex) { _items.CopyTo(array, arrayIndex); } public bool Remove(CommunicatorCollectionItem item) { var removed = _items.Remove(item); if (removed) { // 移除元素后,调整后续元素的索引 UpdateIndexes(); } return removed; } public IEnumerator> GetEnumerator() { return _items.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return _items.GetEnumerator(); } // 通过名称删除 public bool Remove(string name) { var namedItem = Get(name); if (namedItem != null) { return Remove(namedItem); } return false; } // 通过索引删除 public bool Remove(int index) { var namedItem = Get(index); if (namedItem != null) { return Remove(namedItem); } return false; } // 删除项后,调整其他项的索引 private void UpdateIndexes() { for (int i = 0; i < _items.Count; i++) { _items[i].Index = i; } } } public class CommunicatorCollectionItem { public string Name { get; set; } public int Index { get; set; } public T Value { get; set; } // 存储的对象 public CommunicatorCollectionItem(string name, int index, T value) { Name = name; Index = index; Value = value; } public override string ToString() { return $"{Name} (Index: {Index}, Value: {Value})"; } } }