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

来源:互联网 发布:拜纳姆生涯数据 编辑:程序博客网 时间:2024/06/06 00:57

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

             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);新建委托

原创粉丝点击