mirror of
https://github.com/PacktPublishing/Web-Development-with-Blazor-Second-Edition.git
synced 2025-12-06 05:32:03 +01:00
29 lines
683 B
C#
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
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|