如何以编程方式订阅对象的所有事件?
我正在尝试订阅类似 WPF GridView 的第 3 方组件公开的 ALL 事件,以便进行一些调试。除了建议这可能不是调试它的最佳方法以及类似的东西之外,我想知道是否可以做到这一点。
对于路由事件,它可以像这样工作:
var type = tree.GetType();
do
{
var staticFields = type.GetFields(BindingFlags.Static | BindingFlags.Public);
foreach (var staticField in staticFields)
{
if (typeof(RoutedEvent).IsAssignableFrom(staticField.FieldType))
{
tree.AddHandler((RoutedEvent)staticField.GetValue(null), new RoutedEventHandler(OnRoutedEvent), true);
}
}
} while ((type = type.BaseType) != typeof(object)/* && type.FullName.StartsWith("Telerik")*/);
public void OnRoutedEvent(object sender, System.Windows.RoutedEventArgs e)
{
Debug.WriteLine(e.RoutedEvent.ToString());
}
但是,对于典型事件,这似乎不起作用:
var evts = tree.GetType().GetEvents();
foreach (var ev in evts)
{
ev.AddEventHandler(this, new EventHandler(OnEvent));
}
public void OnEvent(object sender, EventArgs e)
{
//..
}
因为它不喜欢委托是 EventHandler 而不是专用类型,或者因为事件处理程序的签名方法不包含专门的 EventArgs 类类型。
这可以以某种方式完成吗?
------------ 稍后编辑 --------- 在所有三种情况下(我的尝试、ds27680 的建议和 Thomas Levesque 的建议),AddEventHandler 调用都会失败:
System.Reflection.TargetException occurred
Message=Object does not match target type.
Source=mscorlib
StackTrace:
at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.EventInfo.AddEventHandler(Object target, Delegate handler)
at Test.MainWindow..ctor() in c:\users\me\documents\visual studio 2010\Projects\Test\Test\MainWindow.xaml.cs:line 39
我猜事件处理程序方法的签名与 EventArgs 类型不完全匹配这一事实导致它失败......
I am trying to subscribe to ALL events exposed by a WPF GridView-like 3rd party component in order to do some debugging. Aside from the suggestion that this might not be the best way to go about debugging it and stuff like that I would like to know if this can be done.
For the routed events it worked ok like this :
var type = tree.GetType();
do
{
var staticFields = type.GetFields(BindingFlags.Static | BindingFlags.Public);
foreach (var staticField in staticFields)
{
if (typeof(RoutedEvent).IsAssignableFrom(staticField.FieldType))
{
tree.AddHandler((RoutedEvent)staticField.GetValue(null), new RoutedEventHandler(OnRoutedEvent), true);
}
}
} while ((type = type.BaseType) != typeof(object)/* && type.FullName.StartsWith("Telerik")*/);
public void OnRoutedEvent(object sender, System.Windows.RoutedEventArgs e)
{
Debug.WriteLine(e.RoutedEvent.ToString());
}
However, with typical events this doesn't seem to work :
var evts = tree.GetType().GetEvents();
foreach (var ev in evts)
{
ev.AddEventHandler(this, new EventHandler(OnEvent));
}
public void OnEvent(object sender, EventArgs e)
{
//..
}
because it doesn't like either the thing that the delegate is EventHandler instead of the specialized type or because the signature of the event handler method does not contain the specialized EventArgs class type.
Can this be done somehow?
------------ LATER EDIT ---------
In all three cases (my attempt, ds27680's suggestion and Thomas Levesque's suggestion) the AddEventHandler call fails with :
System.Reflection.TargetException occurred
Message=Object does not match target type.
Source=mscorlib
StackTrace:
at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.EventInfo.AddEventHandler(Object target, Delegate handler)
at Test.MainWindow..ctor() in c:\users\me\documents\visual studio 2010\Projects\Test\Test\MainWindow.xaml.cs:line 39
I guess the fact that the event handler method's signature does not match EXACTLY the EventArgs type is what makes it fail...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要将委托“转换”为适当的类型:
You need to "convert" the delegate to the appropriate type:
您可以尝试:
then:
其中 getEventArgs 看起来像这样:
当然缺少很多错误检查/处理......
You could try:
then:
where getEventArgs looks like this:
Of course a lot of error checking/handling is missing...
您可以使用 Delegate.CreateDelegate。值得一试。
You can construct events of a specific type using Delegate.CreateDelegate. Worth a try.