Click or drag to resize

WeakEventSourceTEventArgs Class

A simple weak event source implementation; useful for static events where you don't want to keep a reference to the event sink.
Inheritance Hierarchy
SystemObject
  TomsToolbox.EssentialsWeakEventSourceTEventArgs

Namespace: TomsToolbox.Essentials
Assembly: TomsToolbox.Essentials (in TomsToolbox.Essentials.dll) Version: 2.21.0+44d18b541fc9419ec3c549350a832394661b2a4d
XMLNS for XAML: Not mapped to an xmlns.
Syntax
public class WeakEventSource<TEventArgs>
where TEventArgs : EventArgs

Type Parameters

TEventArgs
The type of the event arguments.

The WeakEventSourceTEventArgs type exposes the following members.

Constructors
 NameDescription
Public methodWeakEventSourceTEventArgsInitializes a new instance of the WeakEventSourceTEventArgs class
Top
Methods
 NameDescription
Public methodRaise Raises the event with the specified sender and argument.
Public methodSubscribe Subscribes the specified handler for the event.
Public methodUnsubscribe Unsubscribes the specified handler from the event.
Top
Extension Methods
 NameDescription
Public Extension MethodSafeCastT Performs a cast from object to T, avoiding possible null violations if T is a value type.
(Defined by ObjectExtensions)
Top
Example
Use like this:
C#
class SampleSource
{
    private readonly WeakEventSource<EventArgs> _source = new WeakEventSource<EventArgs>();

    public event EventHandler AnyAction
    {
        add => _source.Subscribe(value);
        remove => _source.Unsubscribe(value);
    }

    private void OnAnyAction()
    {
        _source.Raise(this, EventArgs.Empty);
    }
}

class SampleSink
{
    public SampleSink()
    {
        var source = new SampleSource();
        source.AnyAction += Source_AnyAction;
    }

    private void Source_AnyAction(object sender, EventArgs e)
    {
        ... do something
    }
}
See Also