PC Review


Reply
Thread Tools Rate Thread

Remove Event Handler without -=

 
 
Franco, Gustavo
Guest
Posts: n/a
 
      3rd Oct 2005
How can I remove all event handler from one event without do -=?

I won't explain why because is too long, and that the only option left I
have right now.

I have one event declared for:

AppDomain.CurrentDomain.AssemblyResolve += XFUNCTION

I need to remove all event handlers for the event AssemblyResolve in the
class CurrentDomain.

I can't do AppDomain.CurrentDomain.AssemblyResolve -= XFUNCTION, because I
don't have the address of the function.

How can it be done?

Thanks,
Gustavo.



 
Reply With Quote
 
 
 
 
Daniel O'Connell [C# MVP]
Guest
Posts: n/a
 
      3rd Oct 2005

"Franco, Gustavo" <gustavo_franco[REMOVEIT]@hotmail.com> wrote in message
news:u%(E-Mail Removed)...
> How can I remove all event handler from one event without do -=?
>
> I won't explain why because is too long, and that the only option left I
> have right now.
>
> I have one event declared for:
>
> AppDomain.CurrentDomain.AssemblyResolve += XFUNCTION
>
> I need to remove all event handlers for the event AssemblyResolve in the
> class CurrentDomain.
>
> I can't do AppDomain.CurrentDomain.AssemblyResolve -= XFUNCTION, because I
> don't have the address of the function.
>
> How can it be done?
>


You can't, really, since you cannot access the raw delegate outside of the
class. You will have to manage it some other way


 
Reply With Quote
 
 
 
 
Marcus Andrén
Guest
Posts: n/a
 
      3rd Oct 2005
On Sun, 2 Oct 2005 20:34:56 -0700, "Franco, Gustavo"
<gustavo_franco[REMOVEIT]@hotmail.com> wrote:

>How can it be done?
>


Try this:

// ********* USE THIS FOR INSTANCE EVENTS ********
// Replace "handler" with the events name
// Replace instance with the object holding the event
//Get eventinfo object so we can call Remove method directly
EventInfo info = instance.GetType().GetEvent("handler");

//"Free" eventhandler so we can call call GetInvocationList
FieldInfo field = instance.GetType().GetField( "handler",
BindingFlags.NonPublic | BindingFlags.Instance );
EventHandler eh = field.GetValue(instance) as EventHandler;

//Remove events one by one
foreach(Delegate del in eh.GetInvocationList())
info.GetRemoveMethod().Invoke(instance, new object[] { del });

// ********* USE THIS FOR STATIC EVENTS ********
// Replace handlerStatic with the events name
// Replace MyClass with the class containing the event
//Get eventinfo object so we can call Remove method directly
EventInfo info2 = typeof(MyClass).GetEvent("handlerStatic");

//"Free" eventhandler so we can call call GetInvocationList
FieldInfo field2 = typeof(MyClass).GetField("handlerStatic",
BindingFlags.NonPublic | BindingFlags.Static);
EventHandler eh2 = field2.GetValue(null) as EventHandler;

//Remove events one by one
foreach (Delegate del in eh2.GetInvocationList())
info2.GetRemoveMethod().Invoke(null, new object[] { del });


Using reflection to remove unknown Events is the only way since it is
impossible to access the EventHandlers methods outside the declaring
class otherwise.

Hopefully this should work in most cases. I have only tested it
briefly.
--
Marcus Andrén

 
Reply With Quote
 
Marcus Andrén
Guest
Posts: n/a
 
      3rd Oct 2005
>Hopefully this should work in most cases. I have only tested it
>briefly.


Have to reply to my own post.

It looks like AppDomain.CurrentDomain.AssemblyResolve doesn't support
removal of events at all so the code I posted won't work.

--
Marcus Andrén
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it any point to make these two event handler into one event handler Tony Johansson Microsoft C# .NET 1 22nd Dec 2012 01:02 AM
Event Handler that creates adds another event handler kaczmar2@gmail.com Microsoft ASP .NET 1 22nd Feb 2007 08:37 AM
Remove Handler.. & Add Handler... VJ Microsoft VB .NET 5 11th Jun 2004 08:57 PM
How to Remove an Event Handler without knowing the name of the handler Charles Law Microsoft VB .NET 4 10th Jun 2004 03:48 PM
Create new event handler or override existing handler? =?Utf-8?B?V29vbiBLaWF0?= Microsoft Dot NET Framework Forms 1 5th Jan 2004 09:08 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:51 PM.