WeakEventSource(TEventArgs) Class
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.CoreWeakEventSourceTEventArgs

Namespace:  TomsToolbox.Core
Assembly:  TomsToolbox.Core (in TomsToolbox.Core.dll) Version: 1.0.72.0
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
  NameDescription
Public Extension MethodSafeCastT
Performs a cast from object to T, avoiding possible null violations if T is a value type.

[CanBeNull]

(Defined by ObjectExtensions.)
Top
Examples
Use like this:
C#
class SampleSource
{
    private readonly WeakEventSource<EventArgs> _source = new WeakEventSource<EventArgs>();

    public event EventHandler AnyAction
    {
        add => _sentEventSource.Subscribe(value);
        remove => _sentEventSource.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