[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

Question regarding event handlers and delegates

Bruce C. Miller

5/21/2008 9:55:00 PM

So, say I have an instance of a .Net object with a collection of
events. It is possible to get at these events and look at them with
various relection-type properties. However, would it be possible to
take another instance of the same type of .Net object, grab all of the
event handlers off of the old object, and stick them onto the new
one?

That is, do have access to these event handler methods (which are
delegates) from just looking at an instantiation of the object, and
can I pull them off and add them as event handlers on another object
of the same type?
2 Answers

Jon Skeet

5/21/2008 10:59:00 PM

0

Bruce C. Miller <bm3719@gmail.com> wrote:
> So, say I have an instance of a .Net object with a collection of
> events. It is possible to get at these events and look at them with
> various relection-type properties. However, would it be possible to
> take another instance of the same type of .Net object, grab all of the
> event handlers off of the old object, and stick them onto the new
> one?

Not necessarily.

> That is, do have access to these event handler methods (which are
> delegates) from just looking at an instantiation of the object, and
> can I pull them off and add them as event handlers on another object
> of the same type?

No - because events are like properties. Just because a class has a
property doesn't mean that property is directly backed by a field, and
the same is true for an event. An event has an "adder" and a "remover"
but they could store the delegate passed into them in a map, or
something like that.

Now *typically* they just use a field - but it's not a reliable way of
doing things.

--
Jon Skeet - <skeet@pobox.com>
Web site: http://www.pobox....
Blog: http://www.msmvps.com...
C# in Depth: http://csharpi...

Pavel Minaev

5/25/2008 8:17:00 PM

0

On May 22, 1:54 am, "Bruce C. Miller" <bm3...@gmail.com> wrote:
> So, say I have an instance of a .Net object with a collection of
> events. It is possible to get at these events and look at them with
> various relection-type properties. However, would it be possible to
> take another instance of the same type of .Net object, grab all of the
> event handlers off of the old object, and stick them onto the new
> one?
>
> That is, do have access to these event handler methods (which are
> delegates) from just looking at an instantiation of the object, and
> can I pull them off and add them as event handlers on another object
> of the same type?

No. The whole point of having events as a distinct entity to
properties/fields of delegate type is encapsulation - only object
itself can inspect the list of event handlers (a particular case of
this is calling all the handlers), and objects of other classes can
only subscribe and unsubscribe.