让集合支持事件

                                        让集合支持事件
                                                   电子科技大学软件学院03级02班 周银辉

    不明白为什么集合没有事件(除了几个特殊的集合:比如AutoCompleteStringCollection 类), 要是集合改变时,比如有元素被添加到集合时,集合能通过事件来通知我们该多好啊. 我们来改造集合.

1, 看看CollcetionBase类

    CollectionBase.PNG
    注意到Insert方法中的this.OnInsert(index, value)和this.OnInsertComplete(index, value)方法, 这很让人很容易联想到引发事件的On*方法. 那么很简单地, 如果我们将this.OnInsert(index, value)重写为如下形式: 
None.gif protected   override   void  OnInsert( int  index,  object  value)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
base.OnInsert(index, value);
InBlock.gif            
this.OnElementInserting(new CollectionChangeEventArgs(CollectionChangeAction.Add, value));
ExpandedBlockEnd.gif        }
其中的OnElementInserting如下:
None.gif   protected   virtual   void  OnElementInserting(CollectionChangeEventArgs arg)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
if (this.ElementInserting != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.ElementInserting(this, arg);
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }
而ElementInserting则恰好是我们定义的插入事件
None.gif public   event  EventHandler < CollectionChangeEventArgs >  ElementInserting;

这样当有元素插入到集合时则会引发我们的ElementInserting事件

2, 完整的代码(这里只演示了插入元素时的相关事件, 其他事件同理)

ContractedBlock.gif ExpandedBlockStart.gif CollectionWithEvents.cs
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.Collections;
None.gif
using System.ComponentModel;
None.gif
None.gif
namespace CollcetionWithEventsDemo
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class CollectionWithEvents<T> : CollectionBase
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
事件#region 事件
InBlock.gif        
public event EventHandler<CollectionChangeEventArgs> ElementInserting;
InBlock.gif        
public event EventHandler<CollectionChangeEventArgs> ElementInserted;
InBlock.gif
InBlock.gif        
protected virtual void OnElementInserting(CollectionChangeEventArgs arg)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (this.ElementInserting != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.ElementInserting(this, arg);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected virtual void OnElementInserted(CollectionChangeEventArgs arg)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (this.ElementInserted != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.ElementInserted(this, arg);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
重写的方法#region 重写的方法
InBlock.gif        
protected override void OnInsert(int index, object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.OnInsert(index, value);
InBlock.gif            
this.OnElementInserting(new CollectionChangeEventArgs(CollectionChangeAction.Add, value));
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnInsertComplete(int index, object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.OnInsertComplete(index, value);
InBlock.gif            
this.OnElementInserted(new CollectionChangeEventArgs(CollectionChangeAction.Add, value));
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
公开的方法#region 公开的方法
InBlock.gif        
public void Insert(int index, T element)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.List.Insert(index, element);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

ContractedBlock.gif ExpandedBlockStart.gif Main.cs
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.ComponentModel;
None.gif
None.gif
namespace CollcetionWithEventsDemo
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            CollectionWithEvents
<int> intList = new CollectionWithEvents<int>();
InBlock.gif
InBlock.gif            intList.ElementInserting 
+= 
InBlock.gif                
new EventHandler<CollectionChangeEventArgs>(intList_ElementInserting);
InBlock.gif            intList.ElementInserted 
+= 
InBlock.gif                
new EventHandler<CollectionChangeEventArgs>(intList_ElementInserted);
InBlock.gif
InBlock.gif            intList.Insert(
01);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
static void intList_ElementInserted(object sender, System.ComponentModel.CollectionChangeEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Console.WriteLine(
"元素 {0} 已经被插入", e.Element);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
static void intList_ElementInserting(object sender, System.ComponentModel.CollectionChangeEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Console.WriteLine(
"元素 {0} 正准备被插入", e.Element);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

运行结果:
CollectionWithEventsDemo.PNG

3, Demo下载 http://files.cnblogs.com/zhouyinhui/CollcetionWithEventsDemo.rar
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值