Edit3:在某个时候,这才开始起作用。不知道为什么。也许是 VS 的一个 bug 被修复了?
Edit2:查看解决方案资源管理器中的分析器节点,我发现当我第一次打开程序时,源生成器成功运行,然后它停止,并且在对代码进行一些更改后,它生成的所有内容都会消失。
immediately after opening solution:
> Analyzers
>> MySourceGenerators
>>> MySourceGenerators.NotifyPropertyChangesGenerator
>>>> _NotifyChangedClass_Notify.cs
after making any edits
> Analyzers
>> MySourceGenerators
>>> MySourceGenerators.NotifyPropertyChangesGenerator
>>>> This generator is not generating files.
Run Code Online (Sandbox Code Playgroud)
编辑:按照注释的建议进行调用后Debugger.Launch(),我可以确认生成器代码正在运行,并且源文本看起来与预期的完全一样。但 IDE 和编译器仍然会给出错误,就好像未包含结果一样。
我正在尝试设置一个源生成器以从本地项目引用运行,但无法让它实际运行。我的 NUnit 测试正在通过,所以我知道实际的生成逻辑很好,但是准系统测试项目既无法编译,又会在 Visual Studio 中报告错误。我正在使用 Visual Studio 2022 Preview 5.0,以防万一。
immediately after opening solution:
> Analyzers
>> MySourceGenerators
>>> MySourceGenerators.NotifyPropertyChangesGenerator
>>>> _NotifyChangedClass_Notify.cs
after making any edits
> Analyzers
>> MySourceGenerators
>>> MySourceGenerators.NotifyPropertyChangesGenerator
>>>> This generator is …Run Code Online (Sandbox Code Playgroud) c# roslyn roslyn-code-analysis sourcegenerators csharp-source-generator