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