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");
            }
            
        }
    }
  • 相关阅读:
    人心散了、项目必然要败(转自CSDN)
    sql server加锁机制
    数据库事物隔离级别
    aop学习
    数据库加锁(转)
    托管代码和非托管代码效率的对比。
    day05 Linux文本处理命令
    day04 CentOS 异常,问题解决方法
    day02 Linux系统介绍与安装
    linux常用命令的英文单词缩写
  • 原文地址:https://www.cnblogs.com/arxive/p/6932445.html
Copyright © 2011-2022 走看看