-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
HelloWorldGenerator.cs
55 lines (50 loc) · 1.72 KB
/
HelloWorldGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
[Generator]
public class HelloWorldGenerator :
ISourceGenerator
{
public void Execute(GeneratorExecutionContext context)
{
var source1 = """
using System;
public static class Helper
{
public static void Method()
{
}
}
""";
context.AddSource("helper", SourceText.From(source1, Encoding.UTF8));
var source2 = """
using System;
public static class HelloWorld
{
public static void SayHello()
{
Console.WriteLine("Hello from generated code!");
}
}
""";
var sourceText = SourceText.From(source2, Encoding.UTF8);
context.AddSource("helloWorld", sourceText);
var descriptor = new DiagnosticDescriptor(
id: "theId",
title: "the title",
messageFormat: "the message from {0}",
category: "the category",
DiagnosticSeverity.Info,
isEnabledByDefault: true);
var location = Location.Create(
Path.Combine("dir", "theFile.cs"),
new(1, 2),
new(
new(1, 2),
new(3, 4)));
var diagnostic = Diagnostic.Create(descriptor, location, "hello world generator");
context.ReportDiagnostic(diagnostic);
}
public void Initialize(GeneratorInitializationContext context)
{
}
}