下面是一个非常简单的源代码生成器示例:https://github.com/nkw/SourceGeneratorIssue/tree/master/SourceGenerators

使用dotnet build成功地编译了两个项目.

> cd ..\SourceGenerators\
> dotnet build
MSBuild version 17.8.3+195e7f5a3 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  SourceGenerators -> C:\source\repos\SourceGenerators\SourceGenerators\bin\Debug\netstandard2.1\SourceGe
  nerators.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.72

> cd ..\ClassLibrary1\
> dotnet build
MSBuild version 17.8.3+195e7f5a3 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  SourceGenerators -> C:\source\repos\SourceGenerators\SourceGenerators\bin\Debug\netstandard2.1\SourceGe
  nerators.dll
  ClassLibrary1 -> C:\source\repos\SourceGenerators\ClassLibrary1\bin\Debug\netstandard2.1\ClassLibrary1.
  dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:01.06

然而,在Visual Studio中,它在Clean Solution之后得到了以下构建错误?

Build started at 3:35 PM...
1>------ Build started: Project: SourceGenerators, Configuration: Debug Any CPU ------
Restored C:\source\repos\SourceGenerators\ClassLibrary1\ClassLibrary1.csproj (in 9 ms).
Restored C:\source\repos\SourceGenerators\SourceGenerators\SourceGenerators.csproj (in 20 ms).
1>C:\source\repos\SourceGenerators\SourceGenerators\Class1.cs(7,14,7,29): warning RS1036: 'ClassLibrary1.SourceGenerator': A project containing analyzers or source generators should specify the property '<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>'
1>SourceGenerators -> C:\source\repos\SourceGenerators\SourceGenerators\bin\Debug\netstandard2.1\SourceGenerators.dll
1>Done building project "SourceGenerators.csproj".
2>------ Build started: Project: ClassLibrary1, Configuration: Debug Any CPU ------
2>CSC : warning CS8032: An instance of analyzer ClassLibrary1.SourceGenerator cannot be created from C:\source\repos\SourceGenerators\SourceGenerators\bin\Debug\netstandard2.1\SourceGenerators.dll : Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified..
2>C:\source\repos\SourceGenerators\ClassLibrary1\Class1.cs(14,36,14,41): error CS8795: Partial method 'Class1.GetS2(string)' must have an implementation part because it has accessibility modifiers.
2>Done building project "ClassLibrary1.csproj" -- FAILED.
========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

推荐答案

根据docs,源代码生成器库必须以netstandard2.0为目标:

源代码生成器项目需要以netstandard2.0 TFM为目标,否则它将无法工作.

非常有趣的是,我能够通过CLI和Rider使用源代码生成器成功地构建应用程序,所以可能这一限制实际上只适用于Visual Studio或通过.NET框架工具构建.

另请参阅this comment @github issue about support for .netstandard 2.1 for source generators:

.NET Standard 2.1可在以下条件下使用:

  • 生成器将在使用.NET工具(例如dotnet builddotnet msbuild)的生成期间工作,但不适用于使用.NET框架工具(例如msbuild)的生成
  • 该生成器不适用于IDE方案,该方案包括在Visual Studio中生成和所有智能感知功能 第二个限制似乎特别有问题,以至于不建议使用这种方案.

Csharp相关问答推荐

需要更改哪些内容才能修复被覆盖的财产中的无效警告CS 8765?

O(N)测试失败

. NET 8 HttpClient post参数将其情况更改为camel'

当我使用NET6作为目标框架时,为什么DotNet使用NET8作为MS包?

如何使用XmlSerializer反序列化字符串数组?

实体核心框架--HasColumnType和HasPrecision有什么不同?

将XPS转换为PDF C#

.NET HttpClient、JsonSerializer或误用的Stream中的内存泄漏?

默认情况下,.NET通用主机(Host.CreateDefaultBuilder)中是否包含UseConsoleLifetime?

如何在毛伊岛应用程序中完美地同步视图模型和视图的加载?

为什么AggregateException的Catch块不足以处理取消?

如果是,我怎么才能让这个加75,如果不是,我怎么才能减go 100?

Blazor Fluent UI DialogService,<;FluentDialogProvider/>;错误

在使用UserManager时,如何包含与其他实体的关系?

在等待OnGetAsync时打开Razor Page显示微调器

为什么在使用JsonDerivedType序列化泛型时缺少$type?

忽略Visual Studio代码中的StyleCop规则

正在try 将自定义字体添加到我的控制台应用程序

使用免费的DotNet库从Azure函数向Azure文件共享上的现有Excel文件追加行

如何对构建在Clean架构和CQRS之上的控制器进行单元测试?