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

S

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
 
B

Ben Voigt

sam.m.gardiner said:
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.
 
G

Guest

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top