3
This commit is contained in:
@@ -6,16 +6,11 @@ namespace MySourceGenerator {
|
||||
[Generator]
|
||||
public class MySourceGenerator : ISourceGenerator {
|
||||
public void Execute(GeneratorExecutionContext context) {
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine("using System;");
|
||||
sb.AppendLine("namespace Generated;");
|
||||
sb.AppendLine("public static class HelloWorld {");
|
||||
sb.AppendLine(" public static string SayHello() {");
|
||||
sb.AppendLine(" return \"Hello World!!!\";");
|
||||
sb.AppendLine(" }");
|
||||
sb.AppendLine("}");
|
||||
|
||||
context.AddSource("HelleWorld.generated", SourceText.From(sb.ToString(), Encoding.UTF8));
|
||||
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));
|
||||
}
|
||||
|
||||
public void Initialize(GeneratorInitializationContext context) {
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Generator\Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
|
||||
<Compile Remove="*/*.template.cs" />
|
||||
<AdditionalFiles Include="*/*.template.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Generated;
|
||||
|
||||
public static class HelloWorld {
|
||||
public static string SayHello() {
|
||||
return "Hello World!!!";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user