zoukankan      html  css  js  c++  java
  • 反射方法获取事件的委托链上的函数

    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");
            }
            
        }
    }
  • 相关阅读:
    js 为表格增加行 动态
    百度测试新搜索结果页面 改进灵感来自谷歌?
    多线程程序中使用fork的问题
    C++ struct和class的区别
    J2SE 5 HotSpot JVM 解释
    并行优化、xvout
    C++基础:纯虚函数和抽象类
    C++的四种cast
    logcat过滤输出
    C++虚函数和纯虚函数(2)
  • 原文地址:https://www.cnblogs.com/arxive/p/6932445.html
Copyright © 2011-2022 走看看