该例子来自于微软官方,虽然讲的很好,但是个人的理解还是非常不到位的。特别是关于委托管理的细节问题,梳理得不是特别清楚。将其摘要于下。也希望牛人将其结构予以较完整的疏离,特别是其中事件触发地方。
该例子是对通过基于集合的泛型类来构建一个自定义的类,并且基于四个方法,重写了其增删改查的操作。
该例子是通过一个改变的改变的通知事件,在每一个方法的最后被触发。其中,DinosaursChangedEventArgs被作为事件信息。
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections.ObjectModel; namespace Collection_Generic2 { class Program { static void Main(string[] args) { //初始化类 dinosaurs dino = new dinosaurs(); dino.Changed += ChangeHandler; //这个地方理解的不是很好 dino.Add("Psiticosaurus"); dino.Add("Caudipteryx"); dino.Add("Compsognathus"); dino.Add("Muttaburrasaurus"); Display(dino); Console.WriteLine("\n IndexOf(\"Muttaburrasaurus\"):{0}", dino.IndexOf("Muttaburrasaurus")); Console.WriteLine("\n Contains(\"Caudipteryx\"):{0}", dino.Contains("Caudipteryx")); Console.WriteLine("\n Insert(2,\"Nanotyrannus\")"); dino.Insert(2,"Nanotyrannus"); Console.WriteLine("\n dino[2]:{0}",dino[2]); Console.WriteLine("\n dino[2]=\"Microraptor\"", dino[2]="Microraptor"); Console.WriteLine("\n Remove (\"Microraptor\")"); dino.Remove("Microraptor"); Console.WriteLine("\n RemoveAt(0)"); dino.RemoveAt(0); Display(dino); Console.ReadLine(); } private static void ChangeHandler(object source, dinosaursChangedEventArgs e) { if (e.ChangeType == ChangeType.Replaced) { Console.WriteLine("{0} was replaced with {1}", e.ChangedItem, e.ReplaceWith); } else if (e.ChangeType == ChangeType.Cleared) { Console.WriteLine("The dinosaur list was cleared"); } else { Console.WriteLine("{0} was {1}", e.ChangedItem, e.ChangeType); } } private static void Display(Collection<string> cs) { Console.WriteLine(); foreach (string item in cs) { Console.WriteLine(item); } } } }
dinosaurs.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections.ObjectModel; namespace Collection_Generic2 { //继承泛型集合 class dinosaurs : Collection<string> { //该处类似于侦查其是否发送改变,一旦发生改变,则触发方法 public event EventHandler<dinosaursChangedEventArgs> Changed; /// <summary> /// 重写InsertItem方法 /// </summary> /// <param name="index"></param> /// <param name="item"></param> protected override void InsertItem(int index, string newItem) { base.InsertItem(index, newItem); EventHandler<dinosaursChangedEventArgs> temp = Changed; if (temp != null)//增强稳健型 { //如果发生改变,那么就添加一个新的项目进入指定位置 //this在这里是什么作用呢?没有想得特别明白 temp(this, new dinosaursChangedEventArgs(ChangeType.Added, newItem, null)); } } /// <summary> /// 这个更换的功能的重写 /// </summary> /// <param name="index"></param> /// <param name="NewItem"></param> protected override void SetItem(int index, string NewItem) { string replaced = Items[index]; base.SetItem(index, NewItem); EventHandler<dinosaursChangedEventArgs> temp = Changed; if (temp != null) { temp(this, new dinosaursChangedEventArgs(ChangeType.Replaced, replaced, NewItem)); } } /// <summary> /// 重写移除操作 /// </summary> /// <param name="index"></param> protected override void RemoveItem(int index) { //Items获取其列表的内容 string removedItem = Items[index]; base.RemoveItem(index); //代理改变这一事件,将其进行赋值 EventHandler<dinosaursChangedEventArgs> temp = Changed; if (temp != null) { temp(this, new dinosaursChangedEventArgs(ChangeType.Removed, removedItem, null)); } } protected override void ClearItems() { base.ClearItems(); EventHandler<dinosaursChangedEventArgs> temp = Changed; if (temp!=null) { temp(this,new dinosaursChangedEventArgs(ChangeType.Cleared,null,null)); } } } /// <summary> /// 改变的事件的参数 /// </summary> public class dinosaursChangedEventArgs : EventArgs { public readonly string ChangedItem; public readonly ChangeType ChangeType; public readonly string ReplaceWith; /// <summary> /// 和类名相同,这样它应该算作重载 /// 其内容是通过重载,对类的三个属性进行赋值 /// </summary> /// <param name="change"></param> /// <param name="item"></param> /// <param name="replacement"></param> public dinosaursChangedEventArgs(ChangeType change, string item, string replacement) { ChangeType = change; ChangedItem = item; ReplaceWith = replacement; } } /// <summary> /// 将类型的四种操作实现枚举,枚举定义后面需要增加分号 /// 因为是一个定义,而非方法或类 /// </summary> public enum ChangeType { Added, Removed, Replaced, Cleared }; }
时间: 2024-10-14 16:54:38