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");
            }
            
        }
    }
  • 相关阅读:
    IP和MAC
    ASCII,Unicode 和 UTF-8
    php(PHP Hypertext Preprocessor)随笔1
    css层叠样式表 (Cascading Style Sheets)初识
    ansible部署
    mysql三种备份方式
    nginx反向代理,负载均衡,动静分离,rewrite地址重写介绍
    Maven安装和配置
    jenkins之Tomcat7+jdk1.7+jenkins
    CentOS 7.0如何安装配置iptables和seLinux以及firewalld
  • 原文地址:https://www.cnblogs.com/arxive/p/6932445.html
Copyright © 2011-2022 走看看