pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/kant2002/SourceGeneratorsKit

dia="all" rel="stylesheet" href="https://github.githubassets.com/assets/primer-0b53c3bf3393e2ed.css" /> GitHub - kant2002/SourceGeneratorsKit: Utility library for faster writing source generators
Skip to content

kant2002/SourceGeneratorsKit

Repository files navigation

Source Generators Toolkit

Nuget

Toolkit for writing source generators, and specifically collect important information about project to aid generation itself.

Getting started

[Generator]
public class MagicGenerator : ISourceGenerator
{
    SyntaxReceiver syntaxReceiver = new MethodsWithAttributeReceiver("MagicAttribute");

    //github.com/ <inheritdoc/>
    public void Initialize(GeneratorInitializationContext context)
    {
        context.RegisterForSyntaxNotifications(() => syntaxReceiver);
    }

    public void Execute(GeneratorExecutionContext context)
    {
        // Retrieve the populated receiver
        if (!(context.SyntaxContextReceiver is SyntaxReceiver receiver))
        {
            return;
        }

        foreach (IMethodSymbol methodSymbol in this.syntaxReceiver.Methods)
        {
            // process your method here.
        }
    }
}

Find all classes which implements specific interface

[Generator]
public class MagicGenerator : ISourceGenerator
{
    SyntaxReceiver syntaxReceiver = new ClassesWithInterfacesReceiver("IEnumerable");

    //github.com/ <inheritdoc/>
    public void Initialize(GeneratorInitializationContext context)
    {
        context.RegisterForSyntaxNotifications(() => syntaxReceiver);
    }

    public void Execute(GeneratorExecutionContext context)
    {
        // Retrieve the populated receiver
        if (!(context.SyntaxContextReceiver is SyntaxReceiver receiver))
        {
            return;
        }

        foreach (INamedTypeSymbol classSymbol in this.syntaxReceiver.Classes)
        {
            // process your class here.
        }
    }
}

Find all classes which derived from well-known type

[Generator]
public class MagicGenerator : ISourceGenerator
{
    SyntaxReceiver syntaxReceiver = new DerivedClassesReceiver("DbConnection");

    //github.com/ <inheritdoc/>
    public void Initialize(GeneratorInitializationContext context)
    {
        context.RegisterForSyntaxNotifications(() => syntaxReceiver);
    }

    public void Execute(GeneratorExecutionContext context)
    {
        // Retrieve the populated receiver
        if (!(context.SyntaxContextReceiver is SyntaxReceiver receiver))
        {
            return;
        }

        foreach (INamedTypeSymbol classSymbol in this.syntaxReceiver.Classes)
        {
            // process your class here.
        }
    }
}

Find all members with given attribute

[Generator]
public class MagicGenerator : ISourceGenerator
{
    SyntaxReceiver syntaxReceiver = new MethodsWithAttributeReceiver("MagicAttribute");

    //github.com/ <inheritdoc/>
    public void Initialize(GeneratorInitializationContext context)
    {
        context.RegisterForSyntaxNotifications(() => syntaxReceiver);
    }

    public void Execute(GeneratorExecutionContext context)
    {
        // Retrieve the populated receiver
        if (!(context.SyntaxContextReceiver is SyntaxReceiver receiver))
        {
            return;
        }

        foreach (IMethodSymbol methodSymbol in this.syntaxReceiver.Methods)
        {
            // process your method here.
            var parameters = methodSymbol.Parameters;
            foreach (var parameter in parameters)
            {
                // Do your parameter magic here.
            }
        }
    }
}

Work with multiple receivers

[Generator]
public class AggregateMagicGenerator : ISourceGenerator
{
    SyntaxReceiver syntaxReceiver1 = new MethodsWithAttributeReceiver("MagicAttribute");
    SyntaxReceiver syntaxReceiver2 = new DerivedClassesReceiver("DbConnection");
    SyntaxReceiver agregateSyntaxReceiver;

    public MagicGenerator()
    {
        this.agregateSyntaxReceiver = new AggregateSyntaxContextReceiver(this.syntaxReceiver1, this.syntaxReceiver2);
    }

    //github.com/ <inheritdoc/>
    public void Initialize(GeneratorInitializationContext context)
    {
        context.RegisterForSyntaxNotifications(() => agregateSyntaxReceiver);
    }

    public void Execute(GeneratorExecutionContext context)
    {
        // Retrieve the populated receiver
        if (!(context.SyntaxContextReceiver is SyntaxReceiver receiver))
        {
            return;
        }

        foreach (IMethodSymbol methodSymbol in this.syntaxReceiver1.Methods)
        {
            // process your method here.
            var parameters = methodSymbol.Parameters;
            foreach (var parameter in parameters)
            {
                // Do your parameter magic here.
            }
        }

        foreach (INamedTypeSymbol classSymbol in this.syntaxReceiver2.Classes)
        {
            // process your class here.
        }
    }
}

About

Utility library for faster writing source generators

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy