以前在CSDN上的Blog写的,通过事件名称给控件动态添加事件,现在转过来(http://blog.csdn.net/qpl007/archive/2005/09/21/486601.aspx)。
为控件动态添加事件用反射做其实很简单,需要四个参数:控件标识(ID),控件类型,事件名称,事件委托。
1
using System.Reflection;
2![]()
3
public void BindMethodToControlEvent(string controlID,Type controlType,string eventName,Delegate method)
4
{
5
EventInfo evInfo = controlType.GetEvent(eventName);
6
FieldInfo conInfo = this.GetType().GetField(controlID,BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
7
if(evInfo != null && conInfo != null)
8
evInfo.AddEventHandler(conInfo.GetValue(this),method);
9
else
10
throw new Exception("无效事件或事件源!");
11
}
using System.Reflection;2

3
public void BindMethodToControlEvent(string controlID,Type controlType,string eventName,Delegate method)4
{5
EventInfo evInfo = controlType.GetEvent(eventName);6
FieldInfo conInfo = this.GetType().GetField(controlID,BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);7
if(evInfo != null && conInfo != null)8
evInfo.AddEventHandler(conInfo.GetValue(this),method);9
else10
throw new Exception("无效事件或事件源!");11
}调用:
1
private void Form_Load(object sender, System.EventArgs e)
2
{
3
this.BindMethodToControlEvent("button_test",typeof(Button),"Click",new EventHandler(this.button_Click));
4
}
5![]()
6
private void button_Click(object sender, System.EventArgs e)
7
{
8
MessageBox.Show("click");
9
}
10![]()
11![]()
private void Form_Load(object sender, System.EventArgs e)2
{3
this.BindMethodToControlEvent("button_test",typeof(Button),"Click",new EventHandler(this.button_Click));4
}5

6
private void button_Click(object sender, System.EventArgs e)7
{8
MessageBox.Show("click");9
}10

11

以上代码在Windows xp pro + .NET Framework 1.1 下测试通过
================================================================
好像还可以继续优化一下,三个参数就够了:控件标识(ID),事件名称,事件委托
因为控件类型可以在方法内得到:
1
public void BindMethodToControlEvent(string controlID,string eventName,Delegate method)
2
{
3
FieldInfo conInfo = this.GetType().GetField(controlID,BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
4
if(conInfo != null)
5
{
6
object source = conInfo.GetValue(this);
7
EventInfo evInfo = source.GetType().GetEvent(eventName);
8
if(evInfo == null)
9
throw new Exception("无效事件名称");
10
evInfo.AddEventHandler(source,method);
11
}
12
else
13
throw new Exception("无效事件源!");
14
}
15![]()
public void BindMethodToControlEvent(string controlID,string eventName,Delegate method)2
{3
FieldInfo conInfo = this.GetType().GetField(controlID,BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);4
if(conInfo != null)5
{6
object source = conInfo.GetValue(this);7
EventInfo evInfo = source.GetType().GetEvent(eventName);8
if(evInfo == null)9
throw new Exception("无效事件名称");10
evInfo.AddEventHandler(source,method);11
}12
else13
throw new Exception("无效事件源!");14
}15

调用:
1
private void Form_Load(object sender, System.EventArgs e)
2
{
3
// button_test 为控件名称
4
// Click 为事件名称
5
this.BindMethodToControlEvent("button_test","Click",new EventHandler(this.button_Click));
6
}
7![]()
private void Form_Load(object sender, System.EventArgs e)2
{3
// button_test 为控件名称4
// Click 为事件名称5
this.BindMethodToControlEvent("button_test","Click",new EventHandler(this.button_Click));6
}7



浙公网安备 33010602011771号