using System.Collections; using System.Collections.Concurrent; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; namespace LB_SmartVision.Tool { public class ObservableConcurrentDictionary : IDictionary { public readonly ConcurrentDictionary Items = new(); public ICollection Keys => ((IDictionary)Items).Keys; public ICollection Values => ((IDictionary)Items).Values; public int Count => ((ICollection>)Items).Count; public bool IsReadOnly => ((ICollection>)Items).IsReadOnly; public event EventHandler> DictionaryChanged; public TValue this[TKey key] { get { return Items.TryGetValue(key, out TValue Value) ? Value : default; } set { //bool exists = Items.TryGetValue(key, out TValue oldValue); if (Items.ContainsKey(key)) Items[key] = value; //if (exists) // OnDictionaryChanged(DictionaryChangeType.Updated, key, key, value, oldValue); } } public bool TryAdd(TKey key, TValue value) { if (Items.ContainsKey(key)) { Items[key] = value; return true; } if (Items.TryAdd(key, value)) { OnDictionaryChanged(DictionaryChangeType.Added, key, key, value, value); return true; } return false; } public bool TryRemove(TKey key, out TValue value) { if (Items.TryRemove(key, out value)) { OnDictionaryChanged(DictionaryChangeType.Removed, key, key, default, value); return true; } return false; } public bool TryRename(TKey oldKey, TKey newKey) { if (Items.TryRemove(oldKey, out TValue value)) { Items.TryAdd(newKey, value); OnDictionaryChanged(DictionaryChangeType.Renamed, newKey, oldKey, value, value); return true; } return false; } protected virtual void OnDictionaryChanged(DictionaryChangeType changeType, TKey newKey, TKey oldKey, TValue newValue, TValue oldValue) { var handlers = DictionaryChanged; if (handlers != null) { foreach (EventHandler> handler in handlers.GetInvocationList()) { try { handler(this, new DictionaryChangedEventArgs(changeType, newKey, oldKey, newValue, oldValue)); } catch (Exception ex) { Debug.WriteLine($"事件处理异常: {ex.Message}"); } } } //DictionaryChanged?.Invoke(this, // new DictionaryChangedEventArgs(changeType, newKey, oldKey, newValue, oldValue)); } public void Add(TKey key, TValue value) { ((IDictionary)Items).Add(key, value); } // 在类中添加类型检查 private static readonly bool IsStringKey = typeof(TKey) == typeof(string); public bool ContainsKey(TKey key) { if (!IsStringKey || key == null) return ((IDictionary)Items).ContainsKey(key); var searchKey = key as string; return ((IDictionary)Items).Keys.Any(k => string.Equals(k as string, searchKey, StringComparison.OrdinalIgnoreCase)); } public bool Remove(TKey key) { return ((IDictionary)Items).Remove(key); } public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) { return ((IDictionary)Items).TryGetValue(key, out value); } public void Add(KeyValuePair item) { ((ICollection>)Items).Add(item); } public void Clear() { foreach (var kvp in Items) OnDictionaryChanged(DictionaryChangeType.Removed, kvp.Key, kvp.Key, default, kvp.Value); ((ICollection>)Items).Clear(); } public bool Contains(KeyValuePair item) { return ((ICollection>)Items).Contains(item); } public void CopyTo(KeyValuePair[] array, int arrayIndex) { ((ICollection>)Items).CopyTo(array, arrayIndex); } public bool Remove(KeyValuePair item) { return ((ICollection>)Items).Remove(item); } public IEnumerator> GetEnumerator() { return ((IEnumerable>)Items).GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable)Items).GetEnumerator(); } // 实现其他IDictionary成员... } public enum DictionaryChangeType { Added, Removed, Renamed, Updated } public class DictionaryChangedEventArgs : EventArgs { public DictionaryChangeType ChangeType { get; } public TKey NewKey { get; } public TKey OldKey { get; } public TValue NewValue { get; } public TValue OldValue { get; } public DictionaryChangedEventArgs(DictionaryChangeType changeType, TKey newKey, TKey oldKey, TValue newValue, TValue oldValue) { ChangeType = changeType; NewKey = newKey; OldKey = oldKey; NewValue = newValue; OldValue = oldValue; } } }