下面是一个非常简单的源代码生成器示例: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 ==========