Page 1 of 1

Check for attached event handler

PostPosted: Tue Mar 23, 2021 1:43 pm
by SBarnes
Is there a way to check if a specific handler has already been attached to an event namely the saveend on an inventory item?

Re: Check for attached event handler  Topic is solved

PostPosted: Tue Mar 23, 2021 5:54 pm
by Mike.Sheen
SBarnes wrote:Is there a way to check if a specific handler has already been attached to an event namely the saveend on an inventory item?


Not outside the class that defines the event you can't.

Inside the Inventory class we could do something like this...

Code: Select all
Action[] handlers = Inventory.SaveEnd.GetInvocationList();


...but as an outsider you cannot do that - it won't even compile.

Perhaps there is another way to achieve what you need... why do you need to know if another handler has already subscribed to that event?

Re: Check for attached event handler

PostPosted: Tue Mar 23, 2021 6:10 pm
by SBarnes
Yes I figured out that GetInvocationList won't compile pretty quickly, what I am trying to do is create a save end and attach it to the inventory object used in the inventory import.

The problem is because you can't get the inventory object until one of my extra destination properties is called but you don't know left to right in the csv what will be first you need to attempt to add the handler in each extra property handler and I am not sure I won't end up with the same handler being called twice but I think I've thought of a work around, and that is try removing the handler first and then add it so that way you only end up with one.

I was just hoping to come up with a clever bit of code to work it out and learn something new into the bargain.

The other option I've thought of is just throw a true/false in in the inventory object's generic object list to say the handler is attached or not, basically my reason for adding the save end is so that I can pluck out my own business object from the generic object list and save the extra stuff that I am adding to the inventory import which is what some of the destination property handlers will be filling in.