Remove Event Handler without -=

  • Thread starter Thread starter Franco, Gustavo
  • Start date Start date
F

Franco, Gustavo

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.
 
Franco said:
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
 
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.
 
Hopefully this should work in most cases. I have only tested it

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top