473,326 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Remove Event Handler without -=

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
3 7452

"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
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
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Srinath Avadhanula | last post by:
Hello, I am wondering if QT has something like QWaitForNextEvent() function. This function would block execution of the application till another key was pressed and then return the event...
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
4
by: Torben | last post by:
For test purposes I attach an event to a control, say a TextBox TextChanged event. At another time the same event delegate is attached to some other control, maybe a listbox. Same event function...
1
by: Tee | last post by:
Hi, Anyone know how to remove a event handler that inherited from another form? I have a base form, and there is a button with click event in the base form. 1 of my another form inherits this...
3
by: Peter Oliphant | last post by:
In general, some controls can have events handlers attached to them, typically via the += operator. How does one remove an event handler added this way? Or, just as good for my purposes, how can...
4
by: Charles Law | last post by:
Is there a way to dynamically remove an event handler from an event without knowing the name of the handler? For example, how can ClassB remove the handler without knowing the name, or how many...
10
by: Franky | last post by:
How Remove All Handler on a event? Like remove all handler on the myButton.Click. Thanks ______________________________ Franky FrankyPDA_NOSPAM_@hotmail.com
4
by: FullBandwidth | last post by:
I have been perusing various blogs and MSDN pages discussing the use of event properties and the EventHandlerList class. I don't believe there's anything special about the EventHandlerList class in...
3
by: Allen Chen [MSFT] | last post by:
Hi Richard, Quote from Richard================================================== However I also want to be able to remove the panes. I have tried to include this, but find that when I first...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.