继承ArrayList,使Add,Remove时触发用户定义事件
给ArrayList类增加一个小功能,使Add,Remove时触发用户定义事件。
1using System;
2using System.Collections;
3using Pierce.AttributeClass;
4
5[assembly:Pierce.AttributeClass.SupportWhatsNew]
6namespace Pierce.EventArrayList
7{
8 /// <summary>
9 /// 扩展的EventArgs类,增加字段Message,用户其他处理
10 /// </summary>
11 [LastModified("2006-01-02","扩展的EventArgs类,增加message字段,用户类EventArrayList处理")]
12 public class ArrayListEventArgs:EventArgs
13 {
14 private string message;
15 public ArrayListEventArgs(string message)
16 {
17 this.message=message;
18 }
19 /// <summary>
20 /// 属性,只读,返回message信息
21 /// </summary>
22 public string Message
23 {
24 get
25 {
26 if(message==null)
27 {
28 throw new Exception("message is null");
29 }
30 return message;
31 }
32
33 }
34 }
35 /// <summary>
36 /// EventArrayList:继承ArrayList类,在Add,Remove使,增加事件处理
37 /// </summary>
38 [LastModified("2006-01-02","继承ArrayList类,在Add,Remove时,增加事件处理")]
39 public class EventArrayList:ArrayList
40 {
41 /// <summary>
42 /// 定义的Event的基础Delegate
43 /// </summary>
44 public delegate void ChangeEventHandler(object sender,ArrayListEventArgs e);
45 /// <summary>
46 /// 定义的Add方法触发的事件
47 /// </summary>
48 public event ChangeEventHandler OnAdd;
49 /// <summary>
50 /// 定义的Remove方法触发的事件
51 /// </summary>
52 public event ChangeEventHandler OnRemove;
53
54 /// <summary>
55 /// 构造函数
56 /// </summary>
57 public EventArrayList():base()
58 {
59
60 }
61
62 /// <summary>
63 /// 重写Add方法,引发用户定义的事件
64 /// </summary>
65 /// <param name="value">增加的对象</param>
66 /// <returns></returns>
67 public override int Add(object value)
68 {
69 ArrayListEventArgs arrayEventargs=new ArrayListEventArgs(value.ToString());
70
71 if(OnAdd!=null && arrayEventargs!=null)
72 {
73 OnAdd(this,arrayEventargs);
74 }
75 return base.Add (value);
76 }
77 /// <summary>
78 /// 重写Remove方法,引发用户定义事件
79 /// </summary>
80 /// <param name="obj">删除的对象</param>
81 public override void Remove(object obj)
82 {
83 ArrayListEventArgs arrayEventargs=new ArrayListEventArgs(obj.ToString());
84
85 if(OnRemove!=null && arrayEventargs!=null)
86 {
87 OnRemove(this,arrayEventargs);
88 }
89 base.Remove (obj);
90 }
91
92
93 }
94}
95
2using System.Collections;
3using Pierce.AttributeClass;
4
5[assembly:Pierce.AttributeClass.SupportWhatsNew]
6namespace Pierce.EventArrayList
7{
8 /// <summary>
9 /// 扩展的EventArgs类,增加字段Message,用户其他处理
10 /// </summary>
11 [LastModified("2006-01-02","扩展的EventArgs类,增加message字段,用户类EventArrayList处理")]
12 public class ArrayListEventArgs:EventArgs
13 {
14 private string message;
15 public ArrayListEventArgs(string message)
16 {
17 this.message=message;
18 }
19 /// <summary>
20 /// 属性,只读,返回message信息
21 /// </summary>
22 public string Message
23 {
24 get
25 {
26 if(message==null)
27 {
28 throw new Exception("message is null");
29 }
30 return message;
31 }
32
33 }
34 }
35 /// <summary>
36 /// EventArrayList:继承ArrayList类,在Add,Remove使,增加事件处理
37 /// </summary>
38 [LastModified("2006-01-02","继承ArrayList类,在Add,Remove时,增加事件处理")]
39 public class EventArrayList:ArrayList
40 {
41 /// <summary>
42 /// 定义的Event的基础Delegate
43 /// </summary>
44 public delegate void ChangeEventHandler(object sender,ArrayListEventArgs e);
45 /// <summary>
46 /// 定义的Add方法触发的事件
47 /// </summary>
48 public event ChangeEventHandler OnAdd;
49 /// <summary>
50 /// 定义的Remove方法触发的事件
51 /// </summary>
52 public event ChangeEventHandler OnRemove;
53
54 /// <summary>
55 /// 构造函数
56 /// </summary>
57 public EventArrayList():base()
58 {
59
60 }
61
62 /// <summary>
63 /// 重写Add方法,引发用户定义的事件
64 /// </summary>
65 /// <param name="value">增加的对象</param>
66 /// <returns></returns>
67 public override int Add(object value)
68 {
69 ArrayListEventArgs arrayEventargs=new ArrayListEventArgs(value.ToString());
70
71 if(OnAdd!=null && arrayEventargs!=null)
72 {
73 OnAdd(this,arrayEventargs);
74 }
75 return base.Add (value);
76 }
77 /// <summary>
78 /// 重写Remove方法,引发用户定义事件
79 /// </summary>
80 /// <param name="obj">删除的对象</param>
81 public override void Remove(object obj)
82 {
83 ArrayListEventArgs arrayEventargs=new ArrayListEventArgs(obj.ToString());
84
85 if(OnRemove!=null && arrayEventargs!=null)
86 {
87 OnRemove(this,arrayEventargs);
88 }
89 base.Remove (obj);
90 }
91
92
93 }
94}
95