Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/havendv/eventgenerator
- Owner: HavenDV
- License: mit
- Created: 2022-07-19T01:20:56.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-22T04:23:26.000Z (about 1 year ago)
- Last Synced: 2024-02-27T07:11:03.622Z (11 months ago)
- Topics: csharp, events, generator, source-generator
- Language: C#
- Homepage:
- Size: 264 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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 enablenamespace 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 enablenamespace 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