Last active
April 25, 2021 15:29
-
-
Save cartermp/64fe1730d68adda454fa1d9455e41656 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using Microsoft.CodeAnalysis; | |
using Microsoft.CodeAnalysis.Text; | |
namespace SourceGeneratorSamples | |
{ | |
[Generator] | |
public class HelloWorldGenerator : ISourceGenerator | |
{ | |
public void Execute(SourceGeneratorContext context) | |
{ | |
// begin creating the source we'll inject into the users compilation | |
var sourceBuilder = new StringBuilder(@" | |
using System; | |
namespace HelloWorldGenerated | |
{ | |
public static class HelloWorld | |
{ | |
public static void SayHello() | |
{ | |
Console.WriteLine(""Hello from generated code!""); | |
Console.WriteLine(""The following syntax trees existed in the compilation that created this program:""); | |
"); | |
// using the context, get a list of syntax trees in the users compilation | |
var syntaxTrees = context.Compilation.SyntaxTrees; | |
// add the filepath of each tree to the class we're building | |
foreach (SyntaxTree tree in syntaxTrees) | |
{ | |
sourceBuilder.AppendLine($@"Console.WriteLine(@"" - {tree.FilePath}"");"); | |
} | |
// finish creating the source to inject | |
sourceBuilder.Append(@" | |
} | |
} | |
}"); | |
// inject the created source into the users compilation | |
context.AddSource("helloWorldGenerator", SourceText.From(sourceBuilder.ToString(), Encoding.UTF8)); | |
} | |
public void Initialize(InitializationContext context) | |
{ | |
// No initialization required for this one | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment