观察控制以确定正在触发的事件? [英] Watch control to determine events being fired?

查看:94
本文介绍了观察控制以确定正在触发的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法列出特定WinForms控件的所有被触发的事件,而不会为每个可能的事件显式创建一个处理程序?例如,我可能希望在各种数据绑定操作期间看到在DataGridView和BindingSource之间触发的事件序列。

Is there a way to list all fired events for specific WinForms controls without explicitly creating a handler for each possible event? For example, I might want to see the sequence of events that fire between a DataGridView and the BindingSource during various databinding actions.

推荐答案

你可以使用反射,但由于涉及到各种事件处理程序的签名,这将会有点棘手。基本上,您必须为类型中的每个事件获取 EventInfo ,并使用 EventHandlerType 属性来计算出在调用< a href =http://msdn.microsoft.com/en-us/library/system.reflection.eventinfo.addeventhandler.aspx =nofollow noreferrer> AddEventHandler Delegate.CreateDelegate 适用于正常事件处理程序模式中的所有内容,但...

You could use reflection, but it's going to be slightly tricky because of the various event handler signatures involved. Basically you'd have to get the EventInfo for each event in the type, and use the EventHandlerType property to work out what delegate type to create before calling AddEventHandler. Delegate.CreateDelegate works for everything that follows the normal event handler pattern though...

这是一个示例应用程序。请注意,它没有进行任何检查 - 如果您给它一些非标准事件,它将抛出异常。您可以很容易地使用反射来打印事件参数。

Here's a sample app. Note that it's not doing any checking - if you give it something with a "non-standard" event it will throw an exception. You could fairly easily use reflection to print out the event args too.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;

namespace ConsoleApp
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Form form = new Form { Size = new Size(400, 200) };
            Button button = new Button { Text = "Click me" };
            form.Controls.Add(button);
            EventSubscriber.SubscribeAll(button);
            Application.Run(form);
        }
    }

    class EventSubscriber
    {
        private static readonly MethodInfo HandleMethod = 
            typeof(EventSubscriber)
                .GetMethod("HandleEvent", 
                           BindingFlags.Instance | 
                           BindingFlags.NonPublic);

        private readonly EventInfo evt;

        private EventSubscriber(EventInfo evt)
        {
            this.evt = evt;
        }

        private void HandleEvent(object sender, EventArgs args)
        {
            Console.WriteLine("Event {0} fired", evt.Name);
        }

        private void Subscribe(object target)
        {
            Delegate handler = Delegate.CreateDelegate(
                evt.EventHandlerType, this, HandleMethod);
            evt.AddEventHandler(target, handler);
        }

        public static void SubscribeAll(object target)
        {
            foreach (EventInfo evt in target.GetType().GetEvents())
            {
                EventSubscriber subscriber = new EventSubscriber(evt);
                subscriber.Subscribe(target);
            }
        }
    }
}

这篇关于观察控制以确定正在触发的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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