C#C
C#3y ago
no >> body

❔ Incremental Generator compilation error

I'm trying to figure out what is wrong with my helloworld generator.

Here is my super simple generator, that adds a generated attribute to the source.

using Microsoft.CodeAnalysis.Text;

namespace HelloGeneratorSourceGenerator;

[Generator]
public class Generator : IIncrementalGenerator
{
    public const string Attribute = @"
namespace HelloGeneratorSourceGenerator.Attributes
{
    [System.AttributeUsage(System.AttributeTargets.Enum)]
    public class EnumCollectionAttribute : System.Attribute
    {
    }
}";

    public void Initialize(IncrementalGeneratorInitializationContext context)
    {
        context.RegisterPostInitializationOutput(
            ctx => ctx.AddSource("EnumCollectionAttribute.g.cs", SourceText.From(Attribute, Encoding.UTF8)));
    }
}


And after adding it I just can use them in my main project like that
using HelloGeneratorSourceGenerator.Attributes;

namespace HelloGeneratorConsole.Entities.Enums;

[EnumCollection]
public enum Foo
{
    One = 0,
    Two = 3
}


The problem is that now I have a compilation errors and since this is my first attempt to use generators, I have no clue what I did wrong. Any ideas?
image.png
Was this page helpful?