2025-10-03 02:10:17 +08:00
|
|
|
using System.Text;
|
|
|
|
|
using Microsoft.CodeAnalysis;
|
|
|
|
|
using Microsoft.CodeAnalysis.Text;
|
|
|
|
|
|
|
|
|
|
namespace MySourceGenerator {
|
|
|
|
|
[Generator]
|
|
|
|
|
public class MySourceGenerator : ISourceGenerator {
|
|
|
|
|
public void Execute(GeneratorExecutionContext context) {
|
2025-10-03 20:08:33 +08:00
|
|
|
var template = context.AdditionalFiles.Where(static file => file.Path.EndsWith("HelloWorld.template.cs")).First().GetText()?.ToString();
|
|
|
|
|
if (template == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
context.AddSource("HelleWorld.generated", SourceText.From(template, Encoding.UTF8));
|
2025-10-03 02:10:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Initialize(GeneratorInitializationContext context) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|