By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,677 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,677 IT Pros & Developers. It's quick & easy.

Remove Event Handler without -=

P: n/a
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.

Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Franco, Gustavo" <gustavo_franco[REMOVEIT]@hotmail.com> wrote in message
news:u%****************@tk2msftngp13.phx.gbl...
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
Nov 17 '05 #2

P: n/a
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

Nov 17 '05 #3

P: n/a
>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
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.