确定事件处理程序的列表必然事件 [英] Determine list of event handlers bound to event

查看:164
本文介绍了确定事件处理程序的列表必然事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WinForms形式,将不会关闭。在OnFormClosing,e.Cancel设置为true。我猜测,在我的应用程序的一些对象绑定到关闭或的FormClosing事件,并阻止紧密。为了找到答案,我想,以确定哪些代表们必然会这些事件中的一个。

I have a WinForms form that won't close. In OnFormClosing, e.Cancel is set to true. I am guessing that some object in my application has bound to the Closing or FormClosing event, and is blocking the close. To find out, I'd like to determine what delegates are bound to one of these events.

有没有一种方法来确定绑定到事件处理程序的列表?理想的情况下我会通过Visual Studio调试器做到这一点,但可以写code在应用程序中,如果必须找到处理程序。了解该事件就像一个隐藏的私人领域,我已经通过调试到非公共领域导航我的形式Windows.Forms.Form的祖先,但无济于事。

Is there a way to determine the list of handlers bound to an event? Ideally I would do this via the Visual Studio debugger, but can write code in the application to find the handlers if necessary. Understanding that an event is like a hidden private field, I've navigated through the Debugger to the "Non-Public Fields" for the "Windows.Forms.Form" ancestor of my form, but to no avail.

推荐答案

在短,你不是要做到这一点 - 但为了调试的目的...

In short, you're not meant to do this - but for debugging purposes...

这是事件的往往由私人领域的支持 - 但不是与控制;他们所使用的 EventHandlerList 办法。你将不得不访问形式的保护活动成员,寻找映射到(私)EVENT_FORMCLOSING对象的对象。

An event is often backed by a private field - but not with controls; they use the EventHandlerList approach. You would have to access the form's protected Events member, looking for the object mapped to the (private) EVENT_FORMCLOSING object.

一旦你的 FormClosingEventHandler GetInvocationList 应该做的工作。

Once you have the FormClosingEventHandler, GetInvocationList should do the job.


using System;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Forms;
class MyForm : Form
{
    public MyForm()
    { // assume we don't know this...
        Name = "My Form";
        FormClosing += Foo;
        FormClosing += Bar;
    }

    void Foo(object sender, FormClosingEventArgs e) { }
    void Bar(object sender, FormClosingEventArgs e) { }

    static void Main()
    {
        Form form = new MyForm();
        EventHandlerList events = (EventHandlerList)typeof(Component)
            .GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Instance)
            .GetValue(form, null);
        object key = typeof(Form)
            .GetField("EVENT_FORMCLOSING", BindingFlags.NonPublic | BindingFlags.Static)
            .GetValue(null);

        Delegate handlers = events[key];
        foreach (Delegate handler in handlers.GetInvocationList())
        {
            MethodInfo method = handler.Method;
            string name = handler.Target == null ? "" : handler.Target.ToString();
            if (handler.Target is Control) name = ((Control)handler.Target).Name;
            Console.WriteLine(name + "; " + method.DeclaringType.Name + "." + method.Name);
        }
    }
}

这篇关于确定事件处理程序的列表必然事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆