做一家新闻媒体网站多少钱,凡科网怎么修改网站,wordpress 中英插件,建筑工程资质合作前言
在开发中#xff0c;对于内置值类型和string我们可以通过封装属性在Set中监听写入操作#xff0c;但是对于Dictionary、List等就不能监听到Add、Remove等写入操作。 所以一般采取两种方式监听它们的读写操作#xff0c;一种是封装操作方法#xff0c;间接进行监听对于内置值类型和string我们可以通过封装属性在Set中监听写入操作但是对于Dictionary、List等就不能监听到Add、Remove等写入操作。 所以一般采取两种方式监听它们的读写操作一种是封装操作方法间接进行监听第二种就是重写Add和Remove等方法。下面介绍第二种方法。
实现
下面是重写了Dictionary的示例List同理。
using System;
using System.Collections.Generic;// 定义一个委托类型用于处理字典变化的事件
public delegate void DictionaryChangedEventHandlerTKey, TValue(object sender, DictionaryChangedEventArgsTKey, TValue e);// 定义一个事件参数类用于封装字典变化的信息
public class DictionaryChangedEventArgsTKey, TValue : EventArgs
{// 变化的类型可以是Add, Remove, Update或Clearpublic DictionaryChangedType ChangeType { get; set; }// 变化的键public TKey Key { get; set; }// 变化的值public TValue Value { get; set; }// 构造函数public DictionaryChangedEventArgs(DictionaryChangedType changeType, TKey key, TValue value){ChangeType changeType;Key key;Value value;}
}// 定义一个枚举类型用于表示字典变化的类型
public enum DictionaryChangedType
{Init,Add,Remove,Update,Clear,
}// 定义一个继承自DictionaryTKey,TValue的类用于触发字典变化的事件
public class ObservableDictionaryTKey, TValue : DictionaryTKey, TValue
{// 定义一个构造函数用于接受一个IDictionaryTKey, TValue类型的参数public ObservableDictionary(IDictionaryTKey, TValue dictionary){base.Clear();foreach (var item in dictionary){base.Add(item.Key, item.Value);}OnDictionaryChanged(new DictionaryChangedEventArgsTKey, TValue(DictionaryChangedType.Init, default, default));}public ObservableDictionary(){OnDictionaryChanged(new DictionaryChangedEventArgsTKey, TValue(DictionaryChangedType.Init, default, default));}// 定义一个事件用于通知字典变化public event DictionaryChangedEventHandlerTKey, TValue DictionaryChanged;// 重写Add方法用于在添加元素时触发事件public new void Add(TKey key, TValue value){base.Add(key, value);OnDictionaryChanged(new DictionaryChangedEventArgsTKey, TValue(DictionaryChangedType.Add, key, value));}// 重写Remove方法用于在删除元素时触发事件public new bool Remove(TKey key){if (base.TryGetValue(key, out TValue value)){base.Remove(key);OnDictionaryChanged(new DictionaryChangedEventArgsTKey, TValue(DictionaryChangedType.Remove, key, value));return true;}return false;}// 重写索引器用于在更新元素时触发事件public new TValue this[TKey key]{get base[key];set{if (base.ContainsKey(key)){base[key] value;OnDictionaryChanged(new DictionaryChangedEventArgsTKey, TValue(DictionaryChangedType.Update, key, value));}else{Add(key, value);}}}// 重写Clear方法用于在清空字典时触发事件public new void Clear(){base.Clear();OnDictionaryChanged(new DictionaryChangedEventArgsTKey, TValue(DictionaryChangedType.Clear, default, default));}// 定义一个虚方法用于触发事件protected virtual void OnDictionaryChanged(DictionaryChangedEventArgsTKey, TValue e){DictionaryChanged?.Invoke(this, e);}public void ResetSubscriptions(){DictionaryChanged null;}
}使用示例 public void Test(){// 创建一个ObservableDictionary实例var dict new ObservableDictionarystring, int();// 注册事件处理程序用于输出字典变化的信息dict.DictionaryChanged (sender, e) {Debug.Log($ChangeType: {e.ChangeType}, Key: {e.Key}, Value: {e.Value});};// 添加元素dict.Add(a, 1);dict.Add(b, 2);// 删除元素dict.Remove(a);// 更新元素dict[b] 3;dict[c] 4;Dictionarystring, int newDic new Dictionarystring, int{{ acd, 120 }};Debug.LogError(((ObservableDictionarystring, int)newDic).Count);var newDic1 new ObservableDictionarystring, int(newDic);Debug.LogError(newDic1.Count);// 清空字典dict.Clear();}