[UWP] [VB]访问&使用反射删除匿名事件处理程序 [英] [UWP][VB]Accessing & Removing Anonymous Event Handlers Using Reflection

查看:78
本文介绍了[UWP] [VB]访问&使用反射删除匿名事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控件,我想删除特定事件的所有事件处理程序。处理程序是匿名的,所以我不能使用通常的RemoveHandler语句。我希望有一些方法可以使用反射来访问和删除所有当前的
处理程序(使用诸如GetRuntimeEvent和EventInfo之类的东西 https://docs.microsoft.com/en -us / dotnet / api / system.reflection.runtimereflectionextensions.getruntimeevent?view = netframework-4.8),但我似乎无法弄清楚如何找到当前处理程序的
(如果有的话)。我知道每个人都会试着告诉我,如果我需要删除它,我应该只使用非匿名处理程序,但我更喜欢匿名方法,以及我想知道是否有任何处理程序
的事实已被添加。有没有办法检测&删除匿名处理程序?谢谢。



Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

I have a control in which I want to remove all the event handlers for a specific event. The handlers are anonymous, so I cannot use the usual RemoveHandler statement. I was hoping there would be some way to use reflection to access and remove all current handlers (using stuff such as GetRuntimeEvent and EventInfo at https://docs.microsoft.com/en-us/dotnet/api/system.reflection.runtimereflectionextensions.getruntimeevent?view=netframework-4.8), but I cannot seem to figure out how to find (if any currently exist) the current handlers. I know that everyone will try to tell me that I should just use a non-anonymous handler if I will need to remove it, but I prefer anonymous methods, along with the fact that I would like to know if any handlers have already been added. Is there any way to detect & remove anonymous handlers? Thanks.


Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

推荐答案

好吧,删除匿名事件处理程序并不容易,可能的方法是将匿名方法存储在委托变量中,然后将代理人添加到活动中。  一般情况下,如果您必须在代码中稍后取消订阅活动,我们建议您不要使用匿名函数来
订阅活动。

Well, it's not easily to remove anonymous Event Handlers, the possible way is to store the anonymous method in a delegate variable, and then add the delegate to the event.  In general, we recommend that you do not use anonymous functions to subscribe to events if you will have to unsubscribe from the event at some later point in your code.

有关详细信息,请参阅此文档:使用匿名方法订阅事件

For more information, please refer this document:To subscribe to events by using an anonymous method

祝你好运,

Roy


这篇关于[UWP] [VB]访问&使用反射删除匿名事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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