明永成

导航

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Reflection;
using System.ComponentModel;

namespace WindowsFormsApplication12
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
//[STAThread]
//static void Main()
//{
// Application.EnableVisualStyles();
// Application.SetCompatibleTextRenderingDefault(false);
// //Application.Run(new Form1());
//}


static void Main(string[] args)
{
System.Windows.Forms.Button btn = new System.Windows.Forms.Button();
System.Windows.Forms.Button btn1 = new System.Windows.Forms.Button();
btn.Click += new EventHandler(btn_Click);
btn.Click += new EventHandler(btn_Click2);
btn.Click += new EventHandler(btn_Click3);
ChangeDelegate(btn, btn1);
btn1.PerformClick();

}

private static void ChangeDelegate(System.Windows.Forms.Button btn, System.Windows.Forms.Button btn1)
{
Type type = btn.GetType();
PropertyInfo pi = type.GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
EventHandlerList ehl = (EventHandlerList)pi.GetValue(btn, null);
FieldInfo fieldInfo = (typeof(System.Windows.Forms.Control)).GetField("EventClick", BindingFlags.Static | BindingFlags.NonPublic);
Delegate d = ehl[fieldInfo.GetValue(null)];
foreach (Delegate del in d.GetInvocationList())
{
btn1.Click += (EventHandler)del;
System.Diagnostics.Debug.WriteLine(del.Method.Name);
//type.InvokeMember("EventClick", BindingFlags.Default | BindingFlags.InvokeMethod, null, btn, new object[] { });
}
}

static void btn_Click(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Click1");
}

static void btn_Click2(object sender, EventArgs e)
{
Console.WriteLine("Click2");
}

static void btn_Click3(object sender, EventArgs e)
{
Console.WriteLine("Click3");
}
}
}

posted on 2013-12-03 11:20  明永成  阅读(166)  评论(0编辑  收藏  举报