使用反射取消订阅活动 [英] Using reflection to unsubscribe an event

查看:63
本文介绍了使用反射取消订阅活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我正在使用的库,它有一个失效的监听器问题。我认为我只是潜入并且自己做取消订阅,因为我不希望它很快就会修复




所以,我有控制其订阅的事件,它已经订阅了

的VisibleChanged事件,并且有一个名为

的事件处理程序ControlVisibleChanged。


基本上,我想做的是:


myControl.VisibleChanged - = new

EventHandler(myControl.ControlVisibleChanged);


问题是myControl.ControlVisibleChanged是私有的。


那么,我将什么传递给EventHandler构造函数?


谢谢

解决方案

>问题是myControl.ControlVisibleChanged是私有的。


>
那么,我将什么传递给EventHandler构造函数?



如果你拥有正确的权限,你也可以使用Reflection获得一个用于

私有方法的MethodInfo。请确保在检索时包含

BindingFlags.NonPublic。

Mattias


-

Mattias Sj?gren [C#MVP] mattias @ mvps.org
http:/ /www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。


" Mattias Sj?gren" < ma ******************** @ mvps.orgwrote in message

news:%2 ********* ******* @ TK2MSFTNGP02.phx.gbl ...


问题是myControl.ControlVisibleChanged是私有的。

那么,我将什么传递给EventHandler构造函数?



如果您拥有正确的权限,您也可以使用Reflection获得一个用于

私有方法的MethodInfo。请确保在检索时包含

BindingFlags.NonPublic。


Mattias



I可以得到MethodInfo,但是我不能将MethodInfo传递给新的

EventHandler(),因为它不需要方法信息。它通常需要

方法本身,我当然需要以某种方式传递与方法相关的类实例




8月31日下午1:33,pedrito < pixbypedrito at yahoo.comwrote:


我可以获得MethodInfo,但我不能将MethodInfo传递给新的

EventHandler ()因为它不需要方法信息。它通常需要

方法本身,我当然需要以某种方式传递与该方法关联的类实例




Delegate.CreateDelegate你追求的是什么?


Jon


I have a library I''m using that has a lapsed listener issue. I figured I''d
just sneak in and do the unsubscription myself since I don''t expect it to be
fixed any time soon.

So, I have the control whose event its subscribed to and it''s subscribed to
the VisibleChanged event and has an event handler called
ControlVisibleChanged.

So basically, what I want to do is:

myControl.VisibleChanged -= new
EventHandler(myControl.ControlVisibleChanged);

The problem is myControl.ControlVisibleChanged is private.

So, what do I pass to the EventHandler constructor?

Thanks

解决方案

>The problem is myControl.ControlVisibleChanged is private.

>
So, what do I pass to the EventHandler constructor?

If you hav the right permissions you can get a MethodInfo for a
private method with Reflection as well. Just make sure you include
BindingFlags.NonPublic when retrieving it.
Mattias

--
Mattias Sj?gren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


"Mattias Sj?gren" <ma********************@mvps.orgwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...

The problem is myControl.ControlVisibleChanged is private.

So, what do I pass to the EventHandler constructor?


If you hav the right permissions you can get a MethodInfo for a
private method with Reflection as well. Just make sure you include
BindingFlags.NonPublic when retrieving it.
Mattias

I can get the MethodInfo, but I can''t pass the MethodInfo to new
EventHandler() because it doesn''t take a method info. It normally takes the
method itself and I certainly need to pass the class instance associated
with the method, somehow.


On Aug 31, 1:33 pm, "pedrito" <pixbypedrito at yahoo.comwrote:

I can get the MethodInfo, but I can''t pass the MethodInfo to new
EventHandler() because it doesn''t take a method info. It normally takes the
method itself and I certainly need to pass the class instance associated
with the method, somehow.

Is Delegate.CreateDelegate what you''re after?

Jon


这篇关于使用反射取消订阅活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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