473,426 Members | 1,867 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,426 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 7487

"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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.