让集合支持事件
电子科技大学软件学院03级02班 周银辉
不明白为什么集合没有事件(除了几个特殊的集合:比如AutoCompleteStringCollection 类), 要是集合改变时,比如有元素被添加到集合时,集合能通过事件来通知我们该多好啊. 我们来改造集合.
1, 看看CollcetionBase类
注意到Insert方法中的this.OnInsert(index, value)和this.OnInsertComplete(index, value)方法, 这很让人很容易联想到引发事件的On*方法. 那么很简单地, 如果我们将this.OnInsert(index, value)重写为如下形式:
protected
override
void
OnInsert(
int
index,
object
value)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
base.OnInsert(index, value);
this.OnElementInserting(new CollectionChangeEventArgs(CollectionChangeAction.Add, value));
}
其中的OnElementInserting如下:
protected
virtual
void
OnElementInserting(CollectionChangeEventArgs arg)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (this.ElementInserting != null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.ElementInserting(this, arg);
}
}
而ElementInserting则恰好是我们定义的插入事件
public
event
EventHandler
<
CollectionChangeEventArgs
>
ElementInserting;
这样当有元素插入到集合时则会引发我们的ElementInserting事件
2, 完整的代码(这里只演示了插入元素时的相关事件, 其他事件同理)
CollectionWithEvents.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.ComponentModel;
![None.gif](/Images/OutliningIndicators/None.gif)
namespace CollcetionWithEventsDemo
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
{
public class CollectionWithEvents<T> : CollectionBase
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
事件#region 事件
public event EventHandler<CollectionChangeEventArgs> ElementInserting;
public event EventHandler<CollectionChangeEventArgs> ElementInserted;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
protected virtual void OnElementInserting(CollectionChangeEventArgs arg)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.ElementInserting != null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.ElementInserting(this, arg);
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
protected virtual void OnElementInserted(CollectionChangeEventArgs arg)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.ElementInserted != null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.ElementInserted(this, arg);
}
}
#endregion
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
重写的方法#region 重写的方法
protected override void OnInsert(int index, object value)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
base.OnInsert(index, value);
this.OnElementInserting(new CollectionChangeEventArgs(CollectionChangeAction.Add, value));
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
protected override void OnInsertComplete(int index, object value)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
base.OnInsertComplete(index, value);
this.OnElementInserted(new CollectionChangeEventArgs(CollectionChangeAction.Add, value));
}
#endregion
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
公开的方法#region 公开的方法
public void Insert(int index, T element)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.List.Insert(index, element);
}
#endregion
}
}
Main.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
![None.gif](/Images/OutliningIndicators/None.gif)
namespace CollcetionWithEventsDemo
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
{
class Program
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
static void Main(string[] args)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CollectionWithEvents<int> intList = new CollectionWithEvents<int>();
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
intList.ElementInserting +=
new EventHandler<CollectionChangeEventArgs>(intList_ElementInserting);
intList.ElementInserted +=
new EventHandler<CollectionChangeEventArgs>(intList_ElementInserted);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
intList.Insert(0, 1);
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
static void intList_ElementInserted(object sender, System.ComponentModel.CollectionChangeEventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Console.WriteLine("元素 {0} 已经被插入", e.Element);
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
static void intList_ElementInserting(object sender, System.ComponentModel.CollectionChangeEventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Console.WriteLine("元素 {0} 正准备被插入", e.Element);
}
}
}
运行结果:
3, Demo下载
http://files.cnblogs.com/zhouyinhui/CollcetionWithEventsDemo.rar