Web-Development-with-Blazor.../Chapter17/SourceGeneratorDemo/SourceGenerator/HelloSourceGenerator.cs
2023-02-17 15:28:17 +01:00

29 lines
683 B
C#

using Microsoft.CodeAnalysis;
namespace SourceGenerator;
[Generator]
public class HelloSourceGenerator : ISourceGenerator
{
public void Execute(GeneratorExecutionContext context)
{
// Build up the source code
string source = """
namespace BlazorWebAssemblyApp;
public class GeneratedService
{
public string GetHello()
{
return "Hello from generated code";
}
}
""";
// Add the source code to the compilation
context.AddSource($"GeneratedService.g.cs", source);
}
public void Initialize(GeneratorInitializationContext context)
{
// No initialization required for this one
}
}