Re: Clone without copying event handlers
- From: "Damien" <Damien_The_Unbeliever@xxxxxxxxxxx>
- Date: 31 Aug 2005 07:31:42 -0700
John Yale wrote:
> I am cloning an object using MemberwiseClone plus some code of my own to
> perform a deep copy. This is fine except MemberwiseClone copies event
> handlers which I don't want.
>
> How can I remove all event handlers from an object? This needs to be done
> from within the object itself. RemoveHandler is not an option as I have no
> knowledge of what handlers have been installed.
>
> I have seen posts sugesting something like:
>
> [Delegate].RemoveAll(ChildAddedEvent, ChildAddedEvent)
>
> where my event is called 'ChildAdded'
>
> This seems on the right lines and although it compiles, it does not have
> the required effect. Ideally I would like to remove all event handlers
> attached to the object without having to name them individually.
Hi John,
I've done this before:
If Not InstanceAddedEvent is Nothing Then
For Each rb as System.Delegate in
InstanceAddedEvent.GetInvocationList()
RemoveHandler
InstanceAdded,DirectCast(rb,InstanceAddedEventHandler)
Next
End If
(If you want C# instead, it shouldn't be very difficult to adapt)
And it seems to work for me.
Damien
.
- References:
- Clone without copying event handlers
- From: John Yale
- Clone without copying event handlers
- Prev by Date: Running Two Visual Studios at the same time, Dll's are being locked
- Next by Date: Re: Default namespace
- Previous by thread: Clone without copying event handlers
- Next by thread: Timers and Services
- Index(es):