日期:2014-05-18 浏览次数:20796 次
using System; using System.Windows.Forms; using System.Reflection; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string NameSpacePath = "WindowsFormsApplication2"; //要绑定的方法所在命名空间 string ClassPath = "WindowsFormsApplication2.Form1"; //要绑定的方法所属的类 string MethodName="button1_Click"; //要绑定的方法名 MethodInfo mi = Assembly.Load(NameSpacePath).CreateInstance(ClassPath).GetType().GetMethod(MethodName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public); EventHandler ev = (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), this,mi); button1.Click += ev; } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("通过反射加载事件成功!"); } } }