Lightweight implementation of generic EventArgs
You can pull the NuGet package using
Install-Package CynCeyd.GenericEventArgs
or alternatively using the dotnet cli
dotnet add package CynCeyd.GenericEventArgs --version 1.0.1
To create a simple generic EventArgs, define an event like so:
public EventHandler<EventArgs<Foo>> Bar;
When raising the event:
protected void RaiseBar(){
OnBar(new EventArgs<Foo>(new Foo()));
}
protected virtual void OnBar(EventArgs<Foo> e){
Bar?.Invoke(this, e);
}
Please notice that EventArgss property Data has both public getters and setters.
If you prefer an immutable version use ReadOnlyEventArgs. The procedure is identical, except for the protected setter for the Data property.
There is also an implementation of the generic EventArgs that is cancelable. It adds the Property Cancel to the EventArgs and may be processes according to your requirements.
Depending on your needs, you might want to implement the underlying interfaces by yourself:
IEventArgs contains
TEventData Data { get; set; }
IReadOnlyEventArgs contains
TEventData Data { get; }
ICancelEventArgs contains
bool Cancel { get; set; }