反射方法获取事件的委托链上的函数

 

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;


namespace TestDev
{
    class Program
    {
        static void Main(string[] args)
        {
        
            System.Windows.Forms.Button btn = new System.Windows.Forms.Button();
            btn.Click += new EventHandler(btn_Click);
            btn.Click += new EventHandler(btn_Click2);
            btn.Click += new EventHandler(btn_Click3);
            btn.MouseMove += btn_MouseMove;
            btn.MouseMove += btn_MouseMove2;
            
            PropertyInfo pi = btn.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
            EventHandlerList ehl = (EventHandlerList)pi.GetValue(btn, null);//EventClick
            FieldInfo fieldInfo = (typeof(System.Windows.Forms.Control)).GetField("EventMouseMove", BindingFlags.Static | BindingFlags.NonPublic);
            Delegate d = ehl[fieldInfo.GetValue(null)];
            foreach (Delegate del in d.GetInvocationList())
                Console.WriteLine(del.Method.Name);
           

        }

        static void btn_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            throw new NotImplementedException();
        }

        static void btn_MouseMove2(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            throw new NotImplementedException();
        }

        static void btn_Click(object sender, EventArgs e)
        {
            Console.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 @ 2017-06-02 11:24  wenglabs  阅读(420)  评论(0编辑  收藏  举报