Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/havendv/eventgenerator

Generates events, OnEvent() methods and EventArgs classes
https://github.com/havendv/eventgenerator

csharp events generator source-generator

Last synced: 3 months ago
JSON representation

Generates events, OnEvent() methods and EventArgs classes

Awesome Lists containing this project

README

        

# EventGenerator
Generates events, `OnEvent()` and `IDisposable SubscribeToEventName(handler)` methods. Can generate complex EventArgs from multi-type attributes.

## Install
```
Install-Package EventGenerator.Generator
```
```xml

```

## Usage
```cs
[Event("DataChanged", PropertyNames = new [] { "Title", "Namespace" })]
public partial class MyClass
{
}
```
will generate:
```csharp
//HintName: MyClass.Events.DataChanged.generated.cs
#nullable enable

namespace H.Generators.IntegrationTests
{
public partial class MyClass
{
///
///
public event global::System.EventHandler? DataChanged;

///
/// A helper method to subscribe to the DataChanged event.
///
public global::System.IDisposable SubscribeToDataChanged(global::System.EventHandler handler)
{
DataChanged += handler;

return new global::EventGenerator.Disposable(() => DataChanged -= handler);
}

///
/// A helper method to raise the DataChanged event.
///
protected virtual global::H.Generators.IntegrationTests.MyClass.DataChangedEventArgs OnDataChanged(global::H.Generators.IntegrationTests.MyClass.DataChangedEventArgs args)
{
DataChanged?.Invoke(this, args);

return args;
}

///
/// A helper method to raise the DataChanged event.
///
protected virtual global::H.Generators.IntegrationTests.MyClass.DataChangedEventArgs OnDataChanged(
string title,
string @namespace)
{
var args = new global::H.Generators.IntegrationTests.MyClass.DataChangedEventArgs(title, @namespace);
DataChanged?.Invoke(this, args);

return args;
}
}
}
```
```csharp
//HintName: MyClass.EventArgs.DataChangedEventArgs.generated.cs
#nullable enable

namespace H.Generators.IntegrationTests
{
public partial class MyClass
{
///
///
///
public class DataChangedEventArgs : global::System.EventArgs
{
///
///
///
public string Title { get; }

///
///
///
public string Namespace { get; }

///
///
///
public DataChangedEventArgs(string title, string @namespace)
{
Title = title;
Namespace = @namespace;
}

///
///
///
public void Deconstruct(out string title, out string @namespace)
{
title = Title;
@namespace = Namespace;
}

///
///
///
public override string ToString()
{
return $"(Title={Title}, Namespace={Namespace})";
}
}
}
}
```

## Open questions
- Should we use EventArgs for the single type case? This is useful, for example,
if the type of the Cancel property is bool, which allows you to tell the calling code to cancel something.
- Should we always generate EventArgs or allow Action delegates to be used for these cases?

## Notes
To use generic attributes, you need to set up `LangVersion` in your .csproj:
```xml
latest
```
There are also non-Generic attributes here.

## Support
Priority place for bugs: https://github.com/HavenDV/EventGenerator/issues
Priority place for ideas and general questions: https://github.com/HavenDV/EventGenerator/discussions
I also have a Discord support channel:
https://discord.gg/g8u2t9dKgE