Deregistering an event listener

    Date: 07/05/06 (C Sharp)    Keywords: no keywords

    If I register an object as an event listener for an event of another object like this:

        sendingObject.EventName += new DelegateName(this.EventHandlerMethod);

    I can deregister this eventlistener only by doing this:

        sendingObject.EventName -= new DelegateName(this.EventHandlerMethod);

    I now have the problem, that several classes (pages), that get instantiated and thrown away, register, but I also need to deregister them again. I don't want to implement a method in every class (there are dozens of them), so my idea was, that the controlling class sends a deregister to the briefcase object. This would be much simpler than to implement a deregister in every class. The problem is, that the pages aren't the only objects, that register as an event listener, so a EventName = null; is impossible, since it would also remove all listeners, that should remain as listeners.

    The event object (which is of the type of the delegate, e.g. EventHandler) has some nice properties and methods. The method GetInvocationList gives you an array of all registered objects... of type Delegate. But if you want to deregister, you need the event handler of the registered object (in the upper example the new DelegateName(this.EventHandlerMethod);). In an Delegate object of the array I can find the target (the registered object) and the MethodInfo of the event handler method.

    Does anyone have an idea how I can 'create' an event handler to deregister an object? And, no: it wouldn't help if I'd put the deregister in the base class of the pages. The briefcase object keeps a lot of different insurances, that can have several sub-insurances. A page can not only register at the briefcase, but also at the insurance and even at the sub-insurance. Since all insurances and sub-insurances derive from the same base class, it would be much easier to tell the base class to remove all listeners, that are pages.

    Source: http://community.livejournal.com/csharp/68463.html

« Changing test data || Databinding to member objects »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home