© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
8 replies
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)));
    }
}
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
}
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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Incremental Generator
C#CC# / help
3y ago
Incremental generator not outputting generated members
C#CC# / help
2y ago
Compilation issue with source generator
C#CC# / help
14mo ago
Linking incremental generator to the consumer project
C#CC# / help
11mo ago