现在的位置: 首页 > 综合 > 正文

c# 利用反射给控件加载事件(以button为例)

2013年10月06日 ⁄ 综合 ⁄ 共 381字 ⁄ 字号 评论关闭

具体的理论就不说了,上一篇博文大概介绍了,这里就直接上主要的代码吧:

             Control _Button = this.Controls["button1"]; //获取窗体中的button1
             System.Reflection.MethodInfo _Info = this.GetType().GetMethod("button1_Click"); //获取按钮单击事件的方法
             Delegate _Click = Delegate.CreateDelegate(typeof(EventHandler), this, _Info); //新建委托  
             _Button.GetType().GetEvent("Click").AddEventHandler(_Button, _Click);新建委托

抱歉!评论已关闭.