Skip to content

CynCeyd/GenericEventArgs

Repository files navigation

GenericEventArgs

Lightweight implementation of generic EventArgs

How to install

NuGet Package

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

EventArgs and ReadOnlyEventArgs

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.

CancelEventArgs and ReadOnlyCancelEventArgs

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.

Interfaces

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; } 

About

Lightweight implementation of generic EventArgs

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages