Re: Matching Event Handlers in an Interface

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Charles Law (blank_at_nowhere.com)
Date: 06/19/04


Date: Sat, 19 Jun 2004 13:11:03 +0100

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" <blank@nowhere.com> wrote in message
news:ehZO2N6UEHA.1652@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
>
> 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
>
>



Relevant Pages

  • Re: Matching Event Handlers in an Interface
    ... Given the sender object, I cannot for the life of me see how to drill down ... >> The article shows how to find methods on a receiver that match the ... >> methods from the receiver that match the pattern. ... Only if it is do I want to remove the handler. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Matching Event Handlers in an Interface
    ... It loops through the sender events and tries to ... > methods from the receiver that match the pattern. ... Only if it is do I want to remove the handler. ...
    (microsoft.public.dotnet.languages.vb)
  • Matching Event Handlers in an Interface
    ... The article shows how to find methods on a receiver that match the pattern ... OnXXXX given the sender. ... Only if it is do I want to remove the handler. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Removing Event Handlers Question - Rephrased
    ... The article shows how to find methods on a receiver that match the pattern ... OnXXXX given the sender. ... >> detach handlers from events, given only the source class and target ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Neutral Format as a Coupling reduction idea that doesnt work.
    ... There are potentially unlimited and unexpected side effects for both sender and receiver. ... That definition defines the semantics of the just the data packet. ... That represents a solution level coupling but at least it is very narrowly focused on the message definition rather than what each side does with the information. ...
    (comp.object)