让范型集合支持事件

    技术2022-05-20  35

    大家看了让集合支持事件 这篇文章是否发现该集合使用的是ArrayList来存储的呢?大家都知道ArrayList这个性能不怎么好,所以今天我们来实现List<T>和Dictionary<TKey, TValue>的事件子类

    List<T>代码 :

    using System; using System.Text; namespace System.Collections.Generic { public class CollectionChangeEventArgs<T> : EventArgs { public CollectionChangeEventArgs(T value) { Value = value; } public T Value { set; get; } public T OldValue { set; get; } public IEnumerable<T> Collection { set; get; } public int Index { set; get; } public int Count { set; get; } } public class ListWithEvent<T> : List<T> { public event EventHandler<CollectionChangeEventArgs<T>> Adding; public event EventHandler<CollectionChangeEventArgs<T>> Added; public event EventHandler<CollectionChangeEventArgs<T>> Inserting; public event EventHandler<CollectionChangeEventArgs<T>> Inserted; public event EventHandler<CollectionChangeEventArgs<T>> Removing; public event EventHandler<CollectionChangeEventArgs<T>> Removed; public event EventHandler<CollectionChangeEventArgs<T>> Changing; public event EventHandler<CollectionChangeEventArgs<T>> Changed; public new void Add(T item) { if (this.Adding != null) this.Adding(this, new CollectionChangeEventArgs<T>(item)); base.Add(item); if (this.Added != null) this.Added(this, new CollectionChangeEventArgs<T>(item)); } public new void AddRange(IEnumerable<T> collection) { if (this.Adding != null) this.Adding(this, new CollectionChangeEventArgs<T>(default(T)) { Collection = collection }); base.AddRange(collection); if (this.Added != null) this.Added(this, new CollectionChangeEventArgs<T>(default(T)) { Collection = collection }); } public new void Insert(int index, T item) { if (this.Inserting != null) this.Inserting(this, new CollectionChangeEventArgs<T>(item) { Index = index }); base.Insert(index, item); if (this.Inserted != null) this.Inserted(this, new CollectionChangeEventArgs<T>(item) { Index = index }); } public new void InsertRange(int index, IEnumerable<T> collection) { if (this.Inserting != null) this.Inserting(this, new CollectionChangeEventArgs<T>(default(T)) { Collection = collection, Index = index }); base.InsertRange(index, collection); if (this.Inserted != null) this.Inserted(this, new CollectionChangeEventArgs<T>(default(T)) { Collection = collection, Index = index }); } public new bool Remove(T item) { if (this.Removing != null) this.Removing(this, new CollectionChangeEventArgs<T>(item)); bool del = base.Remove(item); if (this.Removed != null) this.Removed(this, new CollectionChangeEventArgs<T>(item)); return del; } public new void RemoveAt(int index) { if (this.Removing != null) this.Removing(this, new CollectionChangeEventArgs<T>(default(T)) { Index = index }); base.RemoveAt(index); if (this.Removed != null) this.Removed(this, new CollectionChangeEventArgs<T>(default(T)) { Index = index }); } public new void RemoveRange(int index, int count) { if (this.Removing != null) this.Removing(this, new CollectionChangeEventArgs<T>(default(T)) { Index = index, Count = count }); base.RemoveRange(index, count); if (this.Removed != null) this.Removed(this, new CollectionChangeEventArgs<T>(default(T)) { Index = index, Count = count }); } public new T this[int index] { set { T temp = base[index]; if (this.Changing != null) this.Changing(this, new CollectionChangeEventArgs<T>(value) { OldValue = temp, Index = index }); base[index] = value; if (this.Changed != null) this.Changed(this, new CollectionChangeEventArgs<T>(value) { OldValue = temp, Index = index }); } get { return base[index]; } } } }

    Dictionary<TKey, TValue>的代码

    using System; using System.Text; namespace System.Collections.Generic { public class DictionaryChangeEventArgs<TKey, TValue> : EventArgs { public DictionaryChangeEventArgs(TKey key, TValue value) { Key = key; Value = value; } public TKey Key { set; get; } public TValue Value { set; get; } public TValue OldValue { set; get; } } public class DictionaryWithEvent<TKey, TValue> : Dictionary<TKey, TValue> { public event EventHandler<DictionaryChangeEventArgs<TKey, TValue>> Adding; public event EventHandler<DictionaryChangeEventArgs<TKey, TValue>> Added; public event EventHandler<DictionaryChangeEventArgs<TKey, TValue>> Removing; public event EventHandler<DictionaryChangeEventArgs<TKey, TValue>> Removed; public event EventHandler<DictionaryChangeEventArgs<TKey, TValue>> Changing; public event EventHandler<DictionaryChangeEventArgs<TKey, TValue>> Changed; public new void Add(TKey key, TValue value) { if (this.Adding != null) this.Adding(this, new DictionaryChangeEventArgs<TKey, TValue>(key, value)); base.Add(key, value); if (this.Added != null) this.Added(this, new DictionaryChangeEventArgs<TKey, TValue>(key, value)); } public new bool Remove(TKey key) { TValue temp = base[key]; if (this.Removing != null) this.Removing(this, new DictionaryChangeEventArgs<TKey, TValue>(key, temp)); bool del = base.Remove(key); if (this.Removed != null) this.Removed(this, new DictionaryChangeEventArgs<TKey, TValue>(key, temp)); return del; } public new TValue this[TKey key] { set { TValue temp = base[key]; if (this.Changing != null) this.Changing(this, new DictionaryChangeEventArgs<TKey, TValue>(key, value) { OldValue = temp }); base[key] = value; if (this.Changed != null) this.Changed(this, new DictionaryChangeEventArgs<TKey, TValue>(key, value) { OldValue = temp }); } get { return base[key]; } } } } 使用代码:

    class Program { static void Main(string[] args) { TestListWithEvent(); TestDictionaryWithEvent(); Console.ReadLine(); } private static void TestDictionaryWithEvent() { DictionaryWithEvent<string, string> dict = new DictionaryWithEvent<string, string>(); dict.Adding += new EventHandler<DictionaryChangeEventArgs<string, string>>(dict_Adding); dict.Added += new EventHandler<DictionaryChangeEventArgs<string, string>>(dict_Added); dict.Removing += new EventHandler<DictionaryChangeEventArgs<string, string>>(dict_Removing); dict.Removed += new EventHandler<DictionaryChangeEventArgs<string, string>>(dict_Removed); dict.Changing += new EventHandler<DictionaryChangeEventArgs<string, string>>(dict_Changing); dict.Changed += new EventHandler<DictionaryChangeEventArgs<string, string>>(dict_Changed); dict.Add("123", "ABC"); Print(dict); dict.Add("456", "EFG"); Print(dict); dict.Remove("123"); Print(dict); dict["456"] = "ABC"; Print(dict); } static void dict_Changed(object sender, DictionaryChangeEventArgs<string, string> e) { Console.WriteLine("已经修改Key:" + e.Key + "新值:" + e.Value+"旧值"+e.OldValue); } static void dict_Changing(object sender, DictionaryChangeEventArgs<string, string> e) { Console.WriteLine("将要修改Key:" + e.Key + "新值:" + e.Value + "旧值" + e.OldValue); } static void dict_Removed(object sender, DictionaryChangeEventArgs<string, string> e) { Console.WriteLine("已经移除Key:" + e.Key + "Value:" + e.Value); } static void dict_Removing(object sender, DictionaryChangeEventArgs<string, string> e) { Console.WriteLine("将要移除Key:" + e.Key + "Value:" + e.Value); } static void dict_Added(object sender, DictionaryChangeEventArgs<string, string> e) { Console.WriteLine("已经添加Key:" + e.Key + "Value:" + e.Value); } static void dict_Adding(object sender, DictionaryChangeEventArgs<string, string> e) { Console.WriteLine("将要添加Key:" + e.Key + "Value:" + e.Value); } #region ListWithEvent private static void TestListWithEvent() { ListWithEvent<int> list = new ListWithEvent<int>(); list.Adding += new EventHandler<CollectionChangeEventArgs<int>>(list_Adding); list.Added += new EventHandler<CollectionChangeEventArgs<int>>(list_Added); list.Changing += new EventHandler<CollectionChangeEventArgs<int>>(list_Changing); list.Changed += new EventHandler<CollectionChangeEventArgs<int>>(list_Changed); list.Removing += new EventHandler<CollectionChangeEventArgs<int>>(list_Removing); list.Removed += new EventHandler<CollectionChangeEventArgs<int>>(list_Removed); list.Inserting += new EventHandler<CollectionChangeEventArgs<int>>(list_Inserting); list.Inserted += new EventHandler<CollectionChangeEventArgs<int>>(list_Inserted); list.Add(5); Print(list); list.AddRange(new int[] { 6, 7, 8, 9, 10 }); Print(list); list.Remove(5); Print(list); list.RemoveAt(1); Print(list); list.RemoveRange(2, 2); Print(list); list.Insert(0, 1); Print(list); list.InsertRange(1, new int[] { 2, 3 }); Print(list); list[0] = 100; Print(list); } static void Print(ListWithEvent<int> list) { Console.WriteLine("集合中现在的元素"); foreach (int item in list) { Console.WriteLine(item.ToString()); } } static void Print(DictionaryWithEvent<string,string> dict) { Console.WriteLine("集合中现在的元素"); foreach (string item in dict.Keys) { Console.WriteLine("Key:"+item+" Value:"+dict[item]); } } static void list_Inserted(object sender, CollectionChangeEventArgs<int> e) { if (e.Collection == null && e.Value != default(int)) Console.WriteLine("已经插入:" + e.Value.ToString() + "开始插入位置" + e.Index.ToString()); else if (e.Value == default(int) && e.Collection != null) { int index = e.Index; foreach (int item in e.Collection) { Console.WriteLine("已经插入:" + item.ToString() + "开始插入位置" + index.ToString()); index += 1; } } } static void list_Inserting(object sender, CollectionChangeEventArgs<int> e) { if (e.Collection == null && e.Value != default(int)) Console.WriteLine("将要插入:" + e.Value.ToString() + "开始插入位置" + e.Index.ToString()); else if (e.Value == default(int) && e.Collection != null) { int index = e.Index; foreach (int item in e.Collection) { Console.WriteLine("将要插入:" + item.ToString() + "开始插入位置" + index.ToString()); index += 1; } } } static void list_Removed(object sender, CollectionChangeEventArgs<int> e) { if (e.Value != default(int) && e.Index < 1) Console.WriteLine("已经移除:" + e.Value.ToString()); else if (e.Index > 0 && e.Count < 0) Console.WriteLine("已经移除的位置:" + e.Index.ToString()); else if (e.Index > 0 && e.Count > 0) Console.WriteLine("已经移除的位置:" + e.Index.ToString() + "移除的个数是:" + e.Count.ToString()); } static void list_Removing(object sender, CollectionChangeEventArgs<int> e) { if (e.Value != default(int) && e.Index < 1) Console.WriteLine("将要移除:" + e.Value.ToString()); else if (e.Index > 0 && e.Count < 0) Console.WriteLine("将要移除的位置:" + e.Index.ToString()); else if (e.Index > 0 && e.Count > 0) Console.WriteLine("将要移除的位置:" + e.Index.ToString() + "移除的个数是:" + e.Count.ToString()); } static void list_Changed(object sender, CollectionChangeEventArgs<int> e) { Console.WriteLine("已经修改旧值:" + e.OldValue.ToString() + "新值" + e.Value.ToString()+"位置"+e.Index.ToString()); } static void list_Changing(object sender, CollectionChangeEventArgs<int> e) { Console.WriteLine("将要修改旧值:" + e.OldValue.ToString() + "新值" + e.Value.ToString() + "位置" + e.Index.ToString()); } static void list_Added(object sender, CollectionChangeEventArgs<int> e) { if (e.Collection == null && e.Value != default(int)) Console.WriteLine("已经添加:" + e.Value.ToString()); else if (e.Value == default(int) && e.Collection != null) foreach (int item in e.Collection) Console.WriteLine("已经添加:" + item.ToString()); } static void list_Adding(object sender, CollectionChangeEventArgs<int> e) { if (e.Collection == null && e.Value != default(int)) Console.WriteLine("将要添加:" + e.Value.ToString()); else if (e.Value == default(int) && e.Collection != null) foreach (int item in e.Collection) Console.WriteLine("将要添加:" + item.ToString()); } #endregion }

    下载地址http://download.csdn.net/source/3330792


    最新回复(0)