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)
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 methodWeakEventSourceTEventArgs
Initializes 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
Examples
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