I referred to this official tutorial: https://learn.microsoft.com/zh-cn/dotnet/csharp/roslyn-sdk/source-generators-overview , but it didn’t work as expected.
The Source Generator project I created contains the following two files:
TestSourceGenerator.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
</ItemGroup>
</Project>
TestGenerator.cs
using Microsoft.CodeAnalysis;
namespace TestSourceGenerator
{
[Generator]
public class TestGenerator : ISourceGenerator
{
public void Execute(GeneratorExecutionContext context)
{
var mainMethod = context.Compilation.GetEntryPoint(context.CancellationToken);
string source = $@"// <auto-generated/>
using System;
namespace {mainMethod.ContainingNamespace.ToDisplayString()}
{{
public static partial class {mainMethod.ContainingType.Name}
{{
static partial void HelloFrom(string name) =>
Console.WriteLine($""Generator says: Hi from '{{name}}'"");
}}
}}
";
var typeName = mainMethod.ContainingType.Name;
context.AddSource($"{typeName}.g.cs", source);
}
public void Initialize(GeneratorInitializationContext context)
{
}
}
}
And then i create a console project to reference it like tutorial:
Test.csproj
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference
Include="..TestSourceGeneratorTestSourceGenerator.csproj"
OutputType="Analyzer"
ReferenceOutputAssembly="false"
/>
</ItemGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Program.cs
namespace Test;
partial class Program
{
static void Main(string[] args)
{
HelloFrom("Generated Code");
}
static partial void HelloFrom(string name);
}
I use dotnet run
in terminel to run this Test project, but there is no output.
At which step did i go wrong?
dotnet –info
.NET SDK:
Version: 7.0.120
Commit: 25b977658c
运行时环境:
OS Name: Windows
OS Version: 10.0.22631
OS Platform: Windows
RID: win10-x64
Base Path: C:Program Filesdotnetsdk7.0.120
Host:
Version: 8.0.8
Architecture: x64
Commit: 08338fcaa5
.NET SDKs installed:
6.0.425 [C:Program Filesdotnetsdk]
7.0.120 [C:Program Filesdotnetsdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.33 [C:Program FilesdotnetsharedMicrosoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.20 [C:Program FilesdotnetsharedMicrosoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.33 [C:Program FilesdotnetsharedMicrosoft.NETCore.App]
Microsoft.NETCore.App 7.0.20 [C:Program FilesdotnetsharedMicrosoft.NETCore.App]
Microsoft.NETCore.App 8.0.8 [C:Program FilesdotnetsharedMicrosoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.33 [C:Program FilesdotnetsharedMicrosoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.20 [C:Program FilesdotnetsharedMicrosoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.8 [C:Program FilesdotnetsharedMicrosoft.WindowsDesktop.App]
Other architectures found:
None
Environment variables:
Not set
global.json file:
Not found
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download
Hacbit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.