HavenDV / EventGenerator

Generates events, OnEvent() methods and EventArgs classes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

EventGenerator

Generated events and OnEvent() methods. Can generate complex EventArgs from multi-type attributes.

Install

Install-Package EventGenerator.Generator
<!-- compile asset is required for attributes. -->
<!-- ExcludeAssets="runtime" excludes EventGenerator.Attributes.dll from your output. -->
<PackageReference Include="EventGenerator.Generator" Version="0.3.3" PrivateAssets="all" ExcludeAssets="runtime" />

Usage

using EventGenerator;

#nullable enable

namespace H.Generators.IntegrationTests;

public class MyArgs : EventArgs
{
}

[Event<MyArgs>("Changed")]
public partial class MyClass
{
}

will generate:

//HintName: MyClass.Events.Changed.generated.cs
#nullable enable

namespace H.Generators.IntegrationTests
{
    public partial class MyClass
    {
        /// <summary>
        /// </summary>
        public event global::System.EventHandler<global::H.Generators.IntegrationTests.MyArgs>? Changed;

        /// <summary>
        /// A helper method to raise the Changed event.
        /// </summary>
        protected global::H.Generators.IntegrationTests.MyArgs OnChanged(global::H.Generators.IntegrationTests.MyArgs args)
        {
            Changed?.Invoke(this, args);

            return args;
        }
    }
}

Notes

To use generic attributes, you need to set up LangVersion in your .csproj:

<LangVersion>preview</LangVersion>

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

About

Generates events, OnEvent() methods and EventArgs classes

License:MIT License


Languages

Language:C# 100.0%