C
Charles Law
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my
question about removing event handlers.
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
question about removing event handlers.
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