Home All Groups Group Topic Archive Search About

VB.NET events creates a hidden delegate, but how do I access it?

Author
26 May 2006 4:46 PM
sam.m.gardiner
I'm working with VB.NET events and I want a way to disconnect all the
handlers of an event. I want to do this in the object that is the
source of the event. This is slightly tricky in VB.Net as the eventing
code is slightly hidden.

when you use events in Vb.Net you type this:
<code>
        Public event MyEvent()
</code>

what the compiler adds for you is some hiddent things something like:
<code>
        Public Delegate MyEventEventHandler()

        Public MyEventEvent as MyEventEventHandler
</code>

Plus you get some extra hidden properties for editing the list of
handlers on the event.

I want to loop through the handlers of each event and remove them so
when I want to dispose of the object there are no references keeping it
alive. This is not so tough, I think. If you do this
<code>
       Me.MyEventEvent.GetInvocationList()
</code>
you get a list of the delegates that have been combined (i.e., a list
of the added event handlers), so you can disconnect them one at a time.
This code works in the debugger, but if you do something like this
<code>
      Me.GetType().GetMembers()
</code>
you get a huge list of stuff that includes MyEvent but does NOT include
MyEventEvent or MyEventEventHandler. Howver, if you just write the code
to do it, knowing the event name, it all works.

What I want is a way to access these generated/hidden/implict event
delegates by using reflection.I've seen some people say that they were
doing the same thing and found out how, but they didn't post the code!

Any pointers (ha!)?

sam

Author
29 May 2006 1:50 PM
Ben Voigt
Show quote
"sam.m.gardiner" <sam.m.gardi***@gmail.com> wrote in message
news:1148661969.564197.120990@g10g2000cwb.googlegroups.com...
> I'm working with VB.NET events and I want a way to disconnect all the
> handlers of an event. I want to do this in the object that is the
> source of the event. This is slightly tricky in VB.Net as the eventing
> code is slightly hidden.
>
> when you use events in Vb.Net you type this:
> <code>
>        Public event MyEvent()
> </code>
>
> what the compiler adds for you is some hiddent things something like:
> <code>
>        Public Delegate MyEventEventHandler()
>
>        Public MyEventEvent as MyEventEventHandler
> </code>
>
> Plus you get some extra hidden properties for editing the list of
> handlers on the event.
>
> I want to loop through the handlers of each event and remove them so
> when I want to dispose of the object there are no references keeping it
> alive. This is not so tough, I think. If you do this
> <code>
>       Me.MyEventEvent.GetInvocationList()
> </code>
> you get a list of the delegates that have been combined (i.e., a list
> of the added event handlers), so you can disconnect them one at a time.
> This code works in the debugger, but if you do something like this
> <code>
>      Me.GetType().GetMembers()
> </code>
> you get a huge list of stuff that includes MyEvent but does NOT include
> MyEventEvent or MyEventEventHandler. Howver, if you just write the code
> to do it, knowing the event name, it all works.
>
> What I want is a way to access these generated/hidden/implict event
> delegates by using reflection.I've seen some people say that they were
> doing the same thing and found out how, but they didn't post the code!

Start with Roeder's .NET Reflector and find out more about these fields.
Maybe they are private, maybe marked specialname.  This will tell you what
BindingFlags to pass to GetMembers to find these secret fields.

Show quote
>
> Any pointers (ha!)?
>
> sam
>
Author
30 May 2006 12:33 PM
William Sullivan
I would think that
MyEvent = null
should release your object's handles on any events that are registered.  Not
sure if it works, but I'd try it and check with Reflector to see if the
compiler is doing some magic on the way to IL...

Show quote
"sam.m.gardiner" wrote:

> I'm working with VB.NET events and I want a way to disconnect all the
> handlers of an event. I want to do this in the object that is the
> source of the event. This is slightly tricky in VB.Net as the eventing
> code is slightly hidden.
>
> when you use events in Vb.Net you type this:
> <code>
>         Public event MyEvent()
> </code>
>
> what the compiler adds for you is some hiddent things something like:
> <code>
>         Public Delegate MyEventEventHandler()
>
>         Public MyEventEvent as MyEventEventHandler
> </code>
>
> Plus you get some extra hidden properties for editing the list of
> handlers on the event.
>
> I want to loop through the handlers of each event and remove them so
> when I want to dispose of the object there are no references keeping it
> alive. This is not so tough, I think. If you do this
> <code>
>        Me.MyEventEvent.GetInvocationList()
> </code>
> you get a list of the delegates that have been combined (i.e., a list
> of the added event handlers), so you can disconnect them one at a time.
> This code works in the debugger, but if you do something like this
> <code>
>       Me.GetType().GetMembers()
> </code>
> you get a huge list of stuff that includes MyEvent but does NOT include
> MyEventEvent or MyEventEventHandler. Howver, if you just write the code
> to do it, knowing the event name, it all works.
>
> What I want is a way to access these generated/hidden/implict event
> delegates by using reflection.I've seen some people say that they were
> doing the same thing and found out how, but they didn't post the code!
>
> Any pointers (ha!)?
>
> sam
>
>

AddThis Social Bookmark Button