WeakEventHandler Type

Mediates an EventHandler event, holding only a weak reference to the event sink, and a weak reference to the event source for removing the event handler. Thus there is no reference created from the event source to the event sink, and both the event sink and the event source can be garbage collected.

Typical use:

            source.Changed += new WeakEventHandler(this.EhHandleChange, source, nameof(source.Changed));
Sometimes it might be neccessary to explicitly use the event handler method of this instance:
            source.Changed += new WeakEventHandler(this.EhHandleChange, source, nameof(source.Changed)).EventSink;
You can even maintain a reference to the WeakActionHandler instance in your event sink instance, in case you have to remove the event handling programmatically:
            _weakEventHandler = new WeakEventHandler(this.EhHandleChange, source, nameof(source.Changed)); // weakEventHandler is an instance variable of this class
            source.Changed += _weakEventHandler;
            .
            .
            .
            source.Changed -= _weakEventHandler;

Constructors

Constructor Description

WeakEventHandler(handler, eventSource, eventName)

Full Usage: WeakEventHandler(handler, eventSource, eventName)

Parameters:
    handler : EventHandler - The event handler method (the event sink).
    eventSource : obj - The object that holds the event source.
    eventName : string - The name of the event.

Initializes a new instance of the WeakEventHandler class.

handler : EventHandler

The event handler method (the event sink).

eventSource : obj

The object that holds the event source.

eventName : string

The name of the event.

WeakEventHandler(handler, eventSourceType, eventName)

Full Usage: WeakEventHandler(handler, eventSourceType, eventName)

Parameters:
    handler : EventHandler - The event handler method (the event sink).
    eventSourceType : Type - The type of object that holds the static event.
    eventName : string - The name of the static event.

Initializes a new instance of the WeakActionHandler class for a static event.

Typical usage:

StaticClass.Changed += new WeakActionHandler(this.EhHandleChange, typeof(StaticClass), nameof(StaticClass.Changed));

handler : EventHandler

The event handler method (the event sink).

eventSourceType : Type

The type of object that holds the static event.

eventName : string

The name of the static event.

Instance members

Instance member Description

this.EventSink

Full Usage: this.EventSink

Parameters:
    sender : obj - Sender of the event.
    e : EventArgs - Event args.

Handles the event from the original source. You must not call this method directly. However, it can be neccessary to use the method reference if the implicit casting fails. See remarks in the description of this class.

sender : obj

Sender of the event.

e : EventArgs

Event args.

this.EventSource

Full Usage: this.EventSource

Returns: obj

Gets the event source. Attention! Use the returned value only locally, otherwise, you will get a strong reference that you wanted to avoid.

Returns: obj

this.Remove

Full Usage: this.Remove

Removes the event handler from the event source, using the stored remove action..

Static members

Static member Description

op_ImplicitweakHandler

Full Usage: op_ImplicitweakHandler

Parameters:
Returns: EventHandler

weakHandler : WeakEventHandler
Returns: EventHandler