在接口中匹配事件处理程序 [英] Matching Event Handlers in an Interface

查看:36
本文介绍了在接口中匹配事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mr" yEaH rIgHt"大约一周前发布了以下链接,以回答我关于删除事件处理程序的
问题。

Mr "yEaH rIgHt" posted the following link about a week ago in answer to my
question about removing event handlers.

http://www.vbinfozine.com/t_bindevt.shtml




继该帖子之后,仍然存在以下问题。


文章介绍了如何在接收器上找到与模式相匹配的方法

OnXXXX给出发件人。它遍历发送者事件并尝试从接收器获得与模式匹配的

方法。对于每一个它找到它

要么为事件添加或删除处理程序。


这是很好的直接映射,但我不喜欢没有

直接映射。例如,虽然在很多情况下我可以很容易地使用这个

模式,但是在某些情况下发送者有两个相似的

事件,其中只有一个特定接收者正在进行服务。


假设我的发件人有一个事件ValueChanged。我的接收器很容易

有一个方法OnValueChanged可以使用上面的技术

链接起来。实际上,我的接收器有一个方法MyControl_ValueChanged,但

这也适用于上面的技术。


问题出现在我的发送者有两个值的地方可以换。在这个

的情况下,它可以引发两个事件:Value1Changed和Value2Changed。我的接收器

只服务其中一个,哪一个是在运行时确定的。

因此,接收器仍然有一个方法MyControl_ValueChanged,因为

它对具有两个值的发送者一无所知。


这打破了模式,因为现在有简单的方法来转换事件

Value2Changed into the方法名称MyControl_ValueChanged。


所以,...


我试图找到循环发送者事件的方法,和以前一样,

但是对于每一个我想获得调用列表,看看我的接收器是否在其中。只有这样我才想删除处理程序。我有这个目标


< code>

Public Shared Sub RemoveHandlers(ByVal sender As Object,ByVal receiver

As对象)


Dim SenderType作为类型

Dim ReceiverType作为类型


Dim dlgts()作为[Delegate]


SenderType = sender.GetType()

ReceiverType = receiver.GetType()


For each ei As EventInfo In SenderType.GetEvents()


''对于发件人可以引发的每个事件,获取调用

list

dlgts = sender.Events.Item(* WHAT GOES

HERE?*)。GetInvocationList()


For each dlgt As [Delegate] in dlgts

如果*在收件人上接受代理测试?*然后

ei.RemoveEventHandler(发件人,dlgt)

结束如果

下一页dlgt


下一页ei


结束次级

< / code>


有人可以建议什么可能取代大写文本?或者,

可以

有人想到另一种达到同样目的的方式吗?


谢谢。


Charles



Following on from that post, the following issues still exist.

The article shows how to find methods on a receiver that match the pattern
OnXXXX given the sender. It loops through the sender events and tries to get
methods from the receiver that match the pattern. For each one it finds it
either adds or removes a handler for the event.

This is great where there is a straight forward mapping, but I don''t have a
straight forward mapping. For example, although I could easily use this
pattern in many cases, there are situations where the sender has two similar
events, only one of which a particular receiver is going to service.

Let''s say that my sender has an event ValueChanged. My receiver could easily
have a method OnValueChanged that could be linked up using the technique
above. As it happens, my receiver has a method MyControl_ValueChanged, but
this also works with the technique above.

The problem arises where my sender has two values that can change. In this
case it can raise two events: Value1Changed and Value2Changed. My receiver
only services one of these, and which one is determined at runtime.
Therefore, the receiver still has a method MyControl_ValueChanged, because
it knows nothing of a sender that has two values.

This breaks the pattern, as there is now easy way to transform the event
Value2Changed into the method name MyControl_ValueChanged.

So, ...

I am trying to find a way of looping through the sender''s events, as before,
but for each one I want to get the invocation list, and see if my receiver
is in it. Only if it is do I want to remove the handler. I have got this far

<code>
Public Shared Sub RemoveHandlers(ByVal sender As Object, ByVal receiver
As Object)

Dim SenderType As Type
Dim ReceiverType As Type

Dim dlgts() As [Delegate]

SenderType = sender.GetType()
ReceiverType = receiver.GetType()

For Each ei As EventInfo In SenderType.GetEvents()

'' For each event that the sender can raise, get the invocation
list
dlgts = sender.Events.Item( *WHAT GOES
HERE?* ).GetInvocationList()

For Each dlgt As [Delegate] In dlgts
If *TEST FOR DELEGATE BEING ON RECEIVER?* Then
ei.RemoveEventHandler(sender, dlgt)
End If
Next dlgt

Next ei

End Sub
</code>

Can someone suggest what might replace the capitalised text? Alternatively,
can
anyone think of another way of achieving the same end?

Thanks.

Charles

推荐答案

请有人看看这个。在我看来,它应该是

可以获取事件的处理程序列表,然后删除给定对象中的任何处理程序

。我只是想不出怎么做。


任何人?


Charles

" Charles Law < BL *** @ nowhere.com>在消息中写道

news:eh ************** @ TK2MSFTNGP09.phx.gbl ...
Could someone have a look at this please. It seems to me that it should be
possible to get a list of handlers for an event and then remove any handler
that is in a given object. I just can''t figure out how to do it.

Anyone?

Charles
"Charles Law" <bl***@nowhere.com> wrote in message
news:eh**************@TK2MSFTNGP09.phx.gbl...
Mr" yEaH rIgHt" ;大约一周前发布了以下链接,以回答关于删除事件处理程序的问题。
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my
question about removing event handlers.
http://www.vbinfozine.com/t_bindevt.shtml
继该帖子之后,仍存在以下问题。

这篇文章展示了如何在接收器上找到符合发送方式的OnXXXX模式的方法。它遍历发送者事件并尝试
http://www.vbinfozine.com/t_bindevt.shtml
Following on from that post, the following issues still exist.

The article shows how to find methods on a receiver that match the pattern
OnXXXX given the sender. It loops through the sender events and tries to



从接收器获取与模式匹配的方法。对于每一个它找到它
要么添加或删除事件的处理程序。

这是很好的直接映射,但我没有
a直接映射。例如,虽然在很多情况下我可以很容易地使用这种模式,但在某些情况下,发送者有两个
类似的事件,其中只有一个是特定接收者要服务的。
<让我们说我的发件人有一个事件ValueChanged。我的接收器可以
轻松拥有一个方法OnValueChanged,可以使用上面的技术链接起来。碰巧的是,我的接收器有一个方法MyControl_ValueChanged,但是
这也适用于上面的技术。

问题出现在我的发送者有两个可以改变的值。在这种情况下,它可以引发两个事件:Value1Changed和Value2Changed。我的接收器只服务其中一个,哪个是在运行时确定的。
因此,接收器仍然有一个方法MyControl_ValueChanged,因为它不知道发送器有两个值。

这打破了模式,因为现在有简单的方法来转换事件
Value2Changed到方法名称MyControl_ValueChanged。

所以,...

我试图找到一种循环传递发送者事件的方法,如
之前,但是对于每一个我想得到调用列表,看看我的接收器是什么
在里面。只有这样我才想删除处理程序。我有这个

< code>
Public Shared Sub RemoveHandlers(ByVal sender As Object,ByVal
receiver as Object)

Dim SenderType类型
Dim ReceiverType类型

Dim dlgts()As [Delegate]

SenderType = sender.GetType()
ReceiverType = receiver.GetType ()

对于每个ei As EventInfo在SenderType.GetEvents()中

''对于发件人可以引发的每个事件,获取调用
list
dlgts = sender.Events.Item(*这里有什么用?*)。获取内容列表()

每个dlgt为[代表]在dlgts
如果* TEST FOR在收件人上代表?*然后
ei.RemoveEventHandler(发件人,dlgt)
结束如果
下一个dlgt

下一个ei

结束Sub
< / code>

有人可以建议什么可能r替换大写文本?
或者,有没有人能想到另一种达到同样目的的方式?

谢谢。

Charles


get methods from the receiver that match the pattern. For each one it finds it
either adds or removes a handler for the event.

This is great where there is a straight forward mapping, but I don''t have a straight forward mapping. For example, although I could easily use this
pattern in many cases, there are situations where the sender has two similar events, only one of which a particular receiver is going to service.

Let''s say that my sender has an event ValueChanged. My receiver could easily have a method OnValueChanged that could be linked up using the technique
above. As it happens, my receiver has a method MyControl_ValueChanged, but
this also works with the technique above.

The problem arises where my sender has two values that can change. In this
case it can raise two events: Value1Changed and Value2Changed. My receiver
only services one of these, and which one is determined at runtime.
Therefore, the receiver still has a method MyControl_ValueChanged, because
it knows nothing of a sender that has two values.

This breaks the pattern, as there is now easy way to transform the event
Value2Changed into the method name MyControl_ValueChanged.

So, ...

I am trying to find a way of looping through the sender''s events, as before, but for each one I want to get the invocation list, and see if my receiver
is in it. Only if it is do I want to remove the handler. I have got this far
<code>
Public Shared Sub RemoveHandlers(ByVal sender As Object, ByVal receiver As Object)

Dim SenderType As Type
Dim ReceiverType As Type

Dim dlgts() As [Delegate]

SenderType = sender.GetType()
ReceiverType = receiver.GetType()

For Each ei As EventInfo In SenderType.GetEvents()

'' For each event that the sender can raise, get the invocation
list
dlgts = sender.Events.Item( *WHAT GOES
HERE?* ).GetInvocationList()

For Each dlgt As [Delegate] In dlgts
If *TEST FOR DELEGATE BEING ON RECEIVER?* Then
ei.RemoveEventHandler(sender, dlgt)
End If
Next dlgt

Next ei

End Sub
</code>

Can someone suggest what might replace the capitalised text? Alternatively, can
anyone think of another way of achieving the same end?

Thanks.

Charles



查尔斯,
每个dlgt为[代表]在dlgts
如果*在接收器上进行代理测试?*然后


请记住,[Delegate]是System.Delegate的简称。那个

System.Delegate是一个带有方法&属性。


我首先要看一下方法&

System.Delegate类可用的属性。

http://msdn.microsoft.com/library/de...ClassTopic.asp


特别是System.Delegate.Target和System.Delegate方法

属性。 System.Delegate.Equals方法可能会有所帮助。


希望这会有所帮助

Jay

Charles Law < BL *** @ nowhere.com>在消息中写道

news:eh ************** @ TK2MSFTNGP09.phx.gbl ... Mr" yEaH rIgHt"大约一周前发布了以下链接,以回答关于删除事件处理程序的问题。
For Each dlgt As [Delegate] In dlgts
If *TEST FOR DELEGATE BEING ON RECEIVER?* Then
Remember that [Delegate] is short hand for System.Delegate. That
System.Delegate is a class with methods & properties.

I would start by looking at the methods & properties available on the
System.Delegate class.

http://msdn.microsoft.com/library/de...ClassTopic.asp

Specifically the System.Delegate.Target and System.Delegate Method
properties. The System.Delegate.Equals method may or may not help.

Hope this helps
Jay
"Charles Law" <bl***@nowhere.com> wrote in message
news:eh**************@TK2MSFTNGP09.phx.gbl... Mr "yEaH rIgHt" posted the following link about a week ago in answer to my
question about removing event handlers.
http://www.vbinfozine.com/t_bindevt.shtml
继该帖子之后,仍存在以下问题。

这篇文章展示了如何在接收器上找到符合发送方式的OnXXXX模式的方法。它遍历发送者事件并尝试
http://www.vbinfozine.com/t_bindevt.shtml
Following on from that post, the following issues still exist.

The article shows how to find methods on a receiver that match the pattern
OnXXXX given the sender. It loops through the sender events and tries to



从接收器获取与模式匹配的方法。对于每一个它找到它
要么添加或删除事件的处理程序。

这是很好的直接映射,但我没有
a直接映射。例如,虽然在很多情况下我可以很容易地使用这种模式,但在某些情况下,发送者有两个
类似的事件,其中只有一个是特定接收者要服务的。
<让我们说我的发件人有一个事件ValueChanged。我的接收器可以
轻松拥有一个方法OnValueChanged,可以使用上面的技术链接起来。碰巧的是,我的接收器有一个方法MyControl_ValueChanged,但是
这也适用于上面的技术。

问题出现在我的发送者有两个可以改变的值。在这种情况下,它可以引发两个事件:Value1Changed和Value2Changed。我的接收器只服务其中一个,哪个是在运行时确定的。
因此,接收器仍然有一个方法MyControl_ValueChanged,因为它不知道发送器有两个值。

这打破了模式,因为现在有简单的方法来转换事件
Value2Changed到方法名称MyControl_ValueChanged。

所以,...

我试图找到一种循环传递发送者事件的方法,如
之前,但是对于每一个我想得到调用列表,看看我的接收器是什么
在里面。只有这样我才想删除处理程序。我有这个

< code>
Public Shared Sub RemoveHandlers(ByVal sender As Object,ByVal
receiver as Object)

Dim SenderType类型
Dim ReceiverType类型

Dim dlgts()As [Delegate]

SenderType = sender.GetType()
ReceiverType = receiver.GetType ()

对于每个ei As EventInfo在SenderType.GetEvents()中

''对于发件人可以引发的每个事件,获取调用
list
dlgts = sender.Events.Item(*这里有什么用?*)。获取内容列表()

每个dlgt为[代表]在dlgts
如果* TEST FOR在收件人上代表?*然后
ei.RemoveEventHandler(发件人,dlgt)
结束如果
下一个dlgt

下一个ei

结束Sub
< / code>

有人可以建议什么可能r替换大写文本?
或者,有没有人能想到另一种达到同样目的的方式?

谢谢。

Charles


get methods from the receiver that match the pattern. For each one it finds it
either adds or removes a handler for the event.

This is great where there is a straight forward mapping, but I don''t have a straight forward mapping. For example, although I could easily use this
pattern in many cases, there are situations where the sender has two similar events, only one of which a particular receiver is going to service.

Let''s say that my sender has an event ValueChanged. My receiver could easily have a method OnValueChanged that could be linked up using the technique
above. As it happens, my receiver has a method MyControl_ValueChanged, but
this also works with the technique above.

The problem arises where my sender has two values that can change. In this
case it can raise two events: Value1Changed and Value2Changed. My receiver
only services one of these, and which one is determined at runtime.
Therefore, the receiver still has a method MyControl_ValueChanged, because
it knows nothing of a sender that has two values.

This breaks the pattern, as there is now easy way to transform the event
Value2Changed into the method name MyControl_ValueChanged.

So, ...

I am trying to find a way of looping through the sender''s events, as before, but for each one I want to get the invocation list, and see if my receiver
is in it. Only if it is do I want to remove the handler. I have got this far
<code>
Public Shared Sub RemoveHandlers(ByVal sender As Object, ByVal receiver As Object)

Dim SenderType As Type
Dim ReceiverType As Type

Dim dlgts() As [Delegate]

SenderType = sender.GetType()
ReceiverType = receiver.GetType()

For Each ei As EventInfo In SenderType.GetEvents()

'' For each event that the sender can raise, get the invocation
list
dlgts = sender.Events.Item( *WHAT GOES
HERE?* ).GetInvocationList()

For Each dlgt As [Delegate] In dlgts
If *TEST FOR DELEGATE BEING ON RECEIVER?* Then
ei.RemoveEventHandler(sender, dlgt)
End If
Next dlgt

Next ei

End Sub
</code>

Can someone suggest what might replace the capitalised text? Alternatively, can
anyone think of another way of achieving the same end?

Thanks.

Charles



你好周杰伦


是的,我认为其中一个将允许我用
$测试平等b $ b接收器。我的第一个障碍是这一行:
Hi Jay

Yes, I think that one of those will allow me to test for equality with the
receiver. My first hurdle, though is this line:
dlgts = sender.Events.Item(*这里有什么?*)。获取定位列表()


鉴于发件人对象,我不能为我的生活看到如何向调用列表深入了解
。 Item成员似乎需要一个对象形状为

的键,但我应该使用什么键?我搜索过并搜索过但是

无法在任何地方找到这样的例子。你能想到什么可能有用吗?


谢谢。


Charles

" Jay B. Harlow [MVP - 展望] < JA ************ @ msn.com>在消息中写道

新闻:Om ************** @ tk2msftngp13.phx.gbl ... Charles,
dlgts = sender.Events.Item( *WHAT GOES HERE?* ).GetInvocationList()
Given the sender object, I cannot for the life of me see how to drill down
to the invocation list. The Item member seems to need a key in the shape of
an object, but what key should I use? I have searched and searched but
cannot find an example of this anywhere. Can you think what might work?

Thanks.

Charles
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Om**************@tk2msftngp13.phx.gbl... Charles,
For Each dlgt作为[代表]在dlgts
如果*在接收方上进行代理测试?*然后
请记住,[Delegate]是System.Delegate的简称。那个
System.Delegate是一个带方法和类的类。属性。

我首先看一下方法&在System.Delegate类上可用的属性。
For Each dlgt As [Delegate] In dlgts
If *TEST FOR DELEGATE BEING ON RECEIVER?* Then
Remember that [Delegate] is short hand for System.Delegate. That
System.Delegate is a class with methods & properties.

I would start by looking at the methods & properties available on the
System.Delegate class.


http://msdn.microsoft.com/library/de...ClassTopic.asp
特别是System.Delegate.Target和System.Delegate方法
属性。 System.Delegate.Equals方法可能会有所帮助。

希望这会有所帮助
Jay

" Charles Law" < BL *** @ nowhere.com>在消息中写道
新闻:eh ************** @ TK2MSFTNGP09.phx.gbl ...


http://msdn.microsoft.com/library/de...ClassTopic.asp
Specifically the System.Delegate.Target and System.Delegate Method
properties. The System.Delegate.Equals method may or may not help.

Hope this helps
Jay
"Charles Law" <bl***@nowhere.com> wrote in message
news:eh**************@TK2MSFTNGP09.phx.gbl...

Mr" yEaH rIgHt"大约一周前发布了以下链接,以回答
关于删除事件处理程序的问题。
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers.
http://www.vbinfozine.com/t_bindevt.shtml
继该帖子之后,仍存在以下问题。

这篇文章展示了如何在接收器上找到与给定发送者的
模式OnXXXX相匹配的方法。它遍历发送者事件并尝试
http://www.vbinfozine.com/t_bindevt.shtml
Following on from that post, the following issues still exist.

The article shows how to find methods on a receiver that match the pattern OnXXXX given the sender. It loops through the sender events and tries to


从接收器中获取与该模式匹配的


get

方法。对于每一个,它找到
它为事件添加或删除一个处理程序。

这是很好的直接映射,但我不是
有a
methods from the receiver that match the pattern. For each one it finds it either adds or removes a handler for the event.

This is great where there is a straight forward mapping, but I don''t have a
直接映射。例如,虽然在很多情况下我可以很容易地使用这种模式,但有些情况下发送者有两个类似
straight forward mapping. For example, although I could easily use this
pattern in many cases, there are situations where the sender has two
事件的


事件,其中只有一个特定的接收器将要服务。

让我们说我的发送者有一个事件ValueChanged。我的接收器可以

events, only one of which a particular receiver is going to service.

Let''s say that my sender has an event ValueChanged. My receiver could


轻松

有一个方法OnValueChanged可以使用上面的技术链接起来。实际上,我的接收器有一个方法MyControl_ValueChanged,
have a method OnValueChanged that could be linked up using the technique
above. As it happens, my receiver has a method MyControl_ValueChanged,



但这也适用于上面的技术。

问题出现在我的发件人身上有两个值可以改变。在
这种情况​​下,它可以引发两个事件:Value1Changed和Value2Changed。我的
接收器只服务其中一个,哪一个是在运行时确定的。
因此,接收器仍然有一个方法MyControl_ValueChanged,
因为它不知道发送器有两个值。

这打破了模式,因为现在有简单的方法来转换事件
Value2Changed到方法名称MyControl_ValueChanged。

所以,...

我试图找到一种循环发送者事件的方式,如


but this also works with the technique above.

The problem arises where my sender has two values that can change. In this case it can raise two events: Value1Changed and Value2Changed. My receiver only services one of these, and which one is determined at runtime.
Therefore, the receiver still has a method MyControl_ValueChanged, because it knows nothing of a sender that has two values.

This breaks the pattern, as there is now easy way to transform the event
Value2Changed into the method name MyControl_ValueChanged.

So, ...

I am trying to find a way of looping through the sender''s events, as


之前,

但是对于每一个我想得到的调用列表,看看我的
接收器是否在其中。只有这样我才想删除处理程序。我有这个
but for each one I want to get the invocation list, and see if my receiver is in it. Only if it is do I want to remove the handler. I have got this



< code>
Public Shared Sub RemoveHandlers(ByVal sender As Object,ByVal

<code>
Public Shared Sub RemoveHandlers(ByVal sender As Object, ByVal


receiver


receiver

作为对象)

Dim SenderType作为类型
Dim ReceiverType作为类型

Dim dlgts()As [Delegate]

SenderType = sender.GetType()
ReceiverType = receiver.GetType()

对于每个ei As EventInfo在SenderType.GetEvents()
<对于发件人可以引发的每个事件,获取
调用列表
dlgts = sender.Events.Item(*这里是什么?*)。GetInvocationList()

对于每个dlgt作为[代表]在dlgts
如果*在接收器上进行代理测试?*然后
ei.RemoveEventHandler(发件人,dlgt)
结束如果<接下来dlgt

下一个e我是

结束子
< / code>

有人可以建议什么可能取代大写文本吗?
As Object)

Dim SenderType As Type
Dim ReceiverType As Type

Dim dlgts() As [Delegate]

SenderType = sender.GetType()
ReceiverType = receiver.GetType()

For Each ei As EventInfo In SenderType.GetEvents()

'' For each event that the sender can raise, get the invocation list
dlgts = sender.Events.Item( *WHAT GOES
HERE?* ).GetInvocationList()

For Each dlgt As [Delegate] In dlgts
If *TEST FOR DELEGATE BEING ON RECEIVER?* Then
ei.RemoveEventHandler(sender, dlgt)
End If
Next dlgt

Next ei

End Sub
</code>

Can someone suggest what might replace the capitalised text?


或者,

可以
任何人想到达到同样目的的另一种方式吗?

谢谢。

Charles
can
anyone think of another way of achieving the same end?

Thanks.

Charles




这篇关于在接口中匹配事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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