zoukankan      html  css  js  c++  java
  • c#如何取得事件注册的方法

    这个问题来自论坛,原文为如何判断事件已经被注册过?

    用反射取出事件绑定的委托实例,然后用GetInvocationList就可以得到所有注册的方法了。

    代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Reflection;
    namespace WindowsApplication19
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                this.Load+=new EventHandler(Form1_Load1);
                this.Load+=new EventHandler(Form1_Load2);
    
                PropertyInfo propertyInfo = (typeof(Form)).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
                EventHandlerList eventHandlerList = (EventHandlerList)propertyInfo.GetValue(this, null);
                FieldInfo fieldInfo = (typeof(Form)).GetField("EVENT_LOAD", BindingFlags.Static | BindingFlags.NonPublic);
    
                Delegate d = eventHandlerList[fieldInfo.GetValue(null)];
    
                if (d != null)
                {
                    foreach (Delegate de in d.GetInvocationList())
                        Console.WriteLine(de.Method.Name);
                }
            }
            private void Form1_Load1(object sender, EventArgs e)
            {
                //什么也不干
            }
            private void Form1_Load2(object sender, EventArgs e)
            {
                //什么也不干
            }
        }
    }
  • 相关阅读:
    厦门大学 ACM 1465 连续数列 三分
    厦门大学 ACM 1437 三分
    南京理工 ACM
    厦门大学 ACM 1466 线段树维护
    LCS N(log (N) )
    hdu 1520
    HDU 2196
    zoj 3710 暴力
    互联网创业盈利模式指南(转)
    map
  • 原文地址:https://www.cnblogs.com/cl1024cl/p/6204936.html
Copyright © 2011-2022 走看看