C# 跟踪对象的所有事件触发
Introduction
The events of any object can be traced, by use of a single class, through the use of .NET Reflection.
Have you tried to understand the patterns of raised events of a complicated object? I needed to work with a very capable, very full-featured grid control from a ISV’s popular control collection. It wasn’t very long before I was very puzzled trying to figure out which of the many events provided by the control were the ones I wanted, and in which order they were raised. I started to write individual event handlers for the events I was interested in, but there were so many I thought I’d be better off generating tracing event handlers for all the events using a nice editor macro. But it turned out there were over 260 events for this grid control, and they were declared at various levels of the class hierarchy. I looked for a better way.
.NET Reflection turned out to be a very easy way to get access to the definition of the class I was interested in—or any other class for that matter. It is a simple process to get all the events that a class declares, and then to attach handlers to any or all of the events.
This article presents the simple-but-general event hooking/tracing class I built, which is provided (with a demo) as a download.
CSFramework翻译:
程序介绍
通过.net反射机制实现跟踪单个类的所有事件触发.
不知你尝试过或了解复杂对象的事件触发模式没有,我想用ISV提供的非常流行的控件集之一全特性表格控件实现这个功能。
(注:ISV=Independent Software Vendors 的英文缩写,意为“独立软件开发商”)
我非常困难的尝试从控件的事件列表中找出我想要的事件及触发顺序,在此之前我并没有花费很长时间. 我开始编写个别的感兴趣的事件处理程序, 也有很多我使用宏编辑器生成的跟踪事件处理程序. 但事实证明,此表格控件有超过260个事件,并且它们在不同的类层级内定义,我想寻找更好的方法。
.NET反射被证明是一个非常简单的方式去访问类的定义或任何其它类的事情,我因此非常感兴趣...
本文简单介绍事件挂钩及跟踪类的事件,这里提供演示程序下载。
程序截屏:
原文:http://www.codeproject.com/KB/cs/eventtracingviareflection.aspx
扫一扫加作者微信