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