473,834 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 MyEventEventHan dler()

Public MyEventEvent as MyEventEventHan dler
</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 .GetInvocationL ist()
</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().Ge tMembers()
</code>
you get a huge list of stuff that includes MyEvent but does NOT include
MyEventEvent or MyEventEventHan dler. 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

May 26 '06 #1
7 3446
Sam,

Any reason that you don't use RemoveHandler?

In VBNet handling events is very easy, it don't need the difficult ways from
C#

http://msdn2.microsoft.com/en-us/ms172928.aspx

Cor

"sam.m.gardiner " <sa************ @gmail.com> schreef in bericht
news:11******** **************@ j33g2000cwa.goo glegroups.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 MyEventEventHan dler()

Public MyEventEvent as MyEventEventHan dler
</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 .GetInvocationL ist()
</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().Ge tMembers()
</code>
you get a huge list of stuff that includes MyEvent but does NOT include
MyEventEvent or MyEventEventHan dler. 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

May 27 '06 #2
On 2006-05-27, Cor Ligthert [MVP] <no************ @planet.nl> wrote:
Sam,

Any reason that you don't use RemoveHandler?
And where would you get the [Delegate] parameter to RemoveHandler?
Remember, he's trying to do this from within the class that declared the
event. IAW, from the Publisher end of things, not the Subscriber end of
things.
In VBNet handling events is very easy, it don't need the difficult ways from
C#

http://msdn2.microsoft.com/en-us/ms172928.aspx

Cor

"sam.m.gardiner " <sa************ @gmail.com> schreef in bericht
news:11******** **************@ j33g2000cwa.goo glegroups.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 MyEventEventHan dler()

Public MyEventEvent as MyEventEventHan dler
</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 .GetInvocationL ist()
</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().Ge tMembers()
</code>
you get a huge list of stuff that includes MyEvent but does NOT include
MyEventEvent or MyEventEventHan dler. 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


May 27 '06 #3
On 2006-05-26, sam.m.gardiner <sa************ @gmail.com> 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 MyEventEventHan dler()

Public MyEventEvent as MyEventEventHan dler
</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 .GetInvocationL ist()
</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().Ge tMembers()
</code>
you get a huge list of stuff that includes MyEvent but does NOT include
MyEventEvent or MyEventEventHan dler. Howver, if you just write the code
to do it, knowing the event name, it all works.

You have to pass appropriate binding flags to get private members of a
class. IAW

Me.GetType().Ge tMembers(Reflec tion.BindingFla gs.NonPublic Or _
Reflection.Bind ingFlags.Instan ce)
This will get you MyEventEvent, but MyEventEventHan dler is a type, not a
member, so if you need it you'll need to do something different.

OTOH, I'm not really sure why using the name of the delegate is such a
bad thing. Are you trying to do this generically, or are you just
trying to stay away from undocumented aspects of the language.

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

May 27 '06 #4
Hello, Sam,

Does this link provide any assistance with your problem?

http://www.devx.com/vb2themax/Article/19835/1763

Cheers,
Randy
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 MyEventEventHan dler()

Public MyEventEvent as MyEventEventHan dler
</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 .GetInvocationL ist()
</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().Ge tMembers()
</code>
you get a huge list of stuff that includes MyEvent but does NOT include
MyEventEvent or MyEventEventHan dler. 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

May 29 '06 #5
Sam,
Are you the author of the source class?

Are you using .NET 2.0?

If you are the author of the source class & are using .NET 2.0 then I would
recommend using custom events.

Private m_myEvent As EventHandler

Public Custom Event MyEvent As EventHandler
AddHandler(ByVa l value As EventHandler)
m_myEvent = DirectCast([Delegate].Combine(m_myEv ent, value),
EventHandler)
End AddHandler

RemoveHandler(B yVal value As EventHandler)
m_myEvent = DirectCast([Delegate].Remove(m_myEve nt, value),
EventHandler)
End RemoveHandler

RaiseEvent(ByVa l sender As Object, ByVal e As System.EventArg s)
m_myEvent.Invok e(sender, e)
End RaiseEvent
End Event

I would consider basing the above on the EventHandlerLis t

Imports System.Componen tModel

Private m_events As EventHandlerLis t

Protected ReadOnly Property Events() As EventHandlerLis t
Get
If m_events Is Nothing Then
m_events = New EventHandlerLis t
End If
Return m_events
End Get
End Property

Private ReadOnly MyEventKey As Object = New Object()

Public Custom Event MyEvent As EventHandler
AddHandler(ByVa l value As EventHandler)
Events.AddHandl er(MyEventKey, value)
End AddHandler

RemoveHandler(B yVal value As EventHandler)
Events.RemoveHa ndler(MyEventKe y, value)
End RemoveHandler

RaiseEvent(ByVa l sender As Object, ByVal e As System.EventArg s)
Dim handers As EventHandler = DirectCast(Even ts(MyEventKey),
EventHandler)
handers.Invoke( sender, e)
End RaiseEvent
End Event

Note: classes that inherit directly or indirectly from
System.Componen tModel.Componen t give you the Events property.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"sam.m.gardiner " <sa************ @gmail.com> wrote in message
news:11******** **************@ j33g2000cwa.goo glegroups.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 MyEventEventHan dler()
|
| Public MyEventEvent as MyEventEventHan dler
| </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 .GetInvocationL ist()
| </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().Ge tMembers()
| </code>
| you get a huge list of stuff that includes MyEvent but does NOT include
| MyEventEvent or MyEventEventHan dler. 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
|
May 29 '06 #6
I am the author of the source class and I am not using .Net 2.

I am trying to create some code that will allow a event-raising class
to remove all of the handlers that are attached to its events. This
will prevent the handlers keeping references to the event-raising class
and allow it to be garbage collected.

I have obtained the reference to the event delegate (as david points
out, this is a nested type) but I need the reference to MyEventEvent
which is the _instance_ of this type that is contained in the class
that contains the list of handlers. Once I have this list I can run
through it and detach them all.

I started doing this a I saw some code the other day where somone had
attached to 5 different events, then had to detach from them all in
order to clean up. Seemed tedious and error prone. I wondered why
no-one had written a thing to do the detaching, then I found that it
was a wee bit tricky. Hence this thread.

Thanks for the article Randy, very informative. I'm not very up on my
IL and I haven't tried to read it yet maybe I should learn... And
david, I have tried something like this as I guessed it was private but
I may have screwed it up. I'm trying to do this without having to know
that the member ius called MyEventEvent, as this seems crappy.
Although, this is very unlikely to change, so would probably work.

If I get it running nicely I'll post it, as it should be only a few
lines.

sam

May 29 '06 #7
Sam,
| This
| will prevent the handlers keeping references to the event-raising class
| and allow it to be garbage collected.
The handler's don't keep a reference to the event-raising class!

The event-raising class keeps an implicit reference to the event handling
class via the Delegate. If you dereference the event source, then it will be
GC in time. If you dereference an event handler, the event source will keep
it around. Which is why its important for event handlers to use
RemoveHandler if they use AddHandler.

The Delegate is a type that contains a reference to the handler type & a
reference to the method in the handler type.

| I started doing this a I saw some code the other day where somone had
| attached to 5 different events, then had to detach from them all in
| order to clean up. Seemed tedious and error prone. I wondered why
| no-one had written a thing to do the detaching, then I found that it
| was a wee bit tricky. Hence this thread.
In the case of Forms handling control events detaching the events are not
necessary as the GC will see that one references the other & GC both when
there are no other references.

As I suggested above the only time you need to "worry" about it is when you
have a handler that can become dereference before the source. In which case
I normally add a method (commonly IDisposable.Dis pose) to the handler type
to have it remove its handlers from the source.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"sam.m.gardiner " <sa************ @gmail.com> wrote in message
news:11******** **************@ j73g2000cwa.goo glegroups.com.. .
|I am the author of the source class and I am not using .Net 2.
|
| I am trying to create some code that will allow a event-raising class
| to remove all of the handlers that are attached to its events. This
| will prevent the handlers keeping references to the event-raising class
| and allow it to be garbage collected.
|
| I have obtained the reference to the event delegate (as david points
| out, this is a nested type) but I need the reference to MyEventEvent
| which is the _instance_ of this type that is contained in the class
| that contains the list of handlers. Once I have this list I can run
| through it and detach them all.
|
| I started doing this a I saw some code the other day where somone had
| attached to 5 different events, then had to detach from them all in
| order to clean up. Seemed tedious and error prone. I wondered why
| no-one had written a thing to do the detaching, then I found that it
| was a wee bit tricky. Hence this thread.
|
| Thanks for the article Randy, very informative. I'm not very up on my
| IL and I haven't tried to read it yet maybe I should learn... And
| david, I have tried something like this as I guessed it was private but
| I may have screwed it up. I'm trying to do this without having to know
| that the member ius called MyEventEvent, as this seems crappy.
| Although, this is very unlikely to change, so would probably work.
|
| If I get it running nicely I'll post it, as it should be only a few
| lines.
|
| sam
|
May 29 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
375
by: Steven Brown | last post by:
I'm trying to figure out how to safely use .NET events/delegates in a thread-safe class. There are a couple problems. One is that the standard "if(EventName != null) EventName(...);" call can fail if the event is emptied of all methods between the two statements, implying that some sort of synchronization between this and removals from EventName is needed. The other problem is that if an event with a set of delegates is in the process...
14
12162
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using events since I never saw it used anywhere in MSDN documentation/samples?! Or it will just break when I upgrade to .NET Framework 2.x in the coming years namespace MyNamespac public delegate void MyDel() public class MyBase public virtual...
8
2047
by: Michael McDowell | last post by:
I'm confused: "why do we need to assign a method to a delegate then assign the delegate to an event why not just assign the method to the events event handler" and "how does making a callback to some method differ from calling the method as per normal"?
18
1458
by: **Developer** | last post by:
I always define events with the parameters ByVal sender As Object, ByVal e As EventArgs Even if they are not used. Seems I read someplace that's the thing to do. So I then do:
16
2909
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener will stay alive. Is this correct? If this is correct, I've got a problem. Let's say I've got an object Customer that has an PurchaseList (Collection) of Purchase objects. Now, these Purchase objects were pulled from a datasource Datasource. The...
30
3663
by: Burkhard | last post by:
Hi, I am new to C# (with long year experience in C++) and I am a bit confused by the language construct of events. What is it I can do with events that I cannot do with delegates? At the moment it seems to me that Microsoft has developed similar functionality via two keywords. I do understand that an event offers better encapsulation as the underlying delegate is private, but is that all ? -- Regards
5
2781
by: Daniel | last post by:
Hey guys When you hook an event (c# 2.0 syntax): myEvent += MyMethodToFire; You need to also unsubscribe it to avoid a resource leak so that the object it is in gets garbage collected like so : myEvent -= MyMethodToFire; That's all fine, but when you use visual studio to create events for objects it never creates an unsubscribing reference, so is it puting in resource leaks? Or is this being cleared somewhere that i am not seeing?
11
3273
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When my object is destroyed, I want my object to un-register this event. If I don't then the object would never be destroyed until the object I passed in the constructor is destroyed. I have implemented a Dispose(), Dispose(bool), and ~Finalize...
5
1430
by: puzzlecracker | last post by:
I took a closer look at delegates and events, and it became apparent that events don't offer any extra functionalities over delegates. I don't even see it as syntactical sugar over "overtly obfuscated" delegate syntax. Moreover, you can accomplish everything with delegates that you can accomplish with events, such as combining instances, etc. Am I missing something here? Thanks
0
9799
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9646
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10219
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9331
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7758
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3081
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.