C#源生成器:如何在生成的代码中调试编译器错误?
我正在尝试
但是,从根本上讲,我在生成的代码中调试错误遇到了很大的麻烦。当我在源生成器中的模板中犯了一个错误并尝试编译时,我可能会在生成的文件中遇到“方法必须具有返回类型”之类的错误。但是,当我双击错误时,它不会带我进入生成的代码。这使得很难看到它出了什么问题。
有一个技巧吗?是否有任何方法可以检查生成的代码时无法编译的代码?更一般而言,在生成代码可用于IntelliSense时以及 时的控制权?
我正在使用Visual Studio Professional 2022版本17.1.6和Resharper 2022.1。
提前致谢!
I am experimenting with C# source generators. I have spent about a day on it, and I find it a very frustrating and painful experience. IntelliSense is extremely unreliable. It occasionally works, but most often it does not, and I have not been able to figure out any system to it. (Restarting Visual Studio does not help.)
But more fundamentally, I have great trouble debugging errors in the generated code. When I make a mistake in the template in the source generator and try to compile, I might get errors like "Method must have a return type" in the generated file. But when I double-click on the error, it doesn't take me to the generated code. That makes it extremely hard to see what is wrong with it.
Is there a trick to it? Is there any way to inspect the generated code when it fails to compile? And more generally, what governs when the generated code is available for IntelliSense and when it is not?
I am using Visual Studio Professional 2022 version 17.1.6 and ReSharper 2022.1.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将以下内容添加到您的代码生成器构造函数代码:
现在您可以在Visual Studio中使用断点。
IntelliSense确实是与代码生成器的VS中的垃圾。您需要关闭VS,删除 /bin和 /obj文件夹,重新打开VS,然后重建。每当您进行更改时,都必须完成此操作。 VS将在您更改某些内容后显示旧版本,IntelliSense将标记新事物是错误的,但是它仍然会编译和运行...
为了获得更好的体验,我建议使用Rider,该骑手为代码生成器提供更好的集成体验。
Add the following to your code generator constructor code:
Now you can use breakpoints in Visual Studio.
Intellisense is indeed garbage in VS with code generators. You need to close VS, delete /bin and /obj folders, reopen VS, then rebuild. This has to be done everytime you make changes. VS will show the old version after you changed something, intellisense will mark new things as error, yet it will still compile and run...
For a better experience I recommend using Rider, which has a much better integration experience for code generators.
我找到了我问题的部分解决方案。
首先,在使用源发电机时,请勿在Visual Studio 中使用Resharper Builder。它没有提供正确的错误消息。而是从命令行运行
dotnet build
。您会收到更好的错误消息。实际上,我的问题的一部分可能是由于我的错误或限制造成的(尽管我没有调查过这一点,所以这是纯粹的猜测)。
其次,您可以在不在编译时间上运行源生成的情况下测试源发电机的内部。。
outputIteMtype =“ Analyszer”
。此外,正如NightOWL888所指出的那样,我可以将其添加到引用项目中,以使其吐出生成的文件到磁盘(
:
我尚未解决Intellisense问题,但是现在我至少可以忍受使用该生成器的工作。
I have found a partial solution to my problem.
First, do not use the ReSharper builder in Visual Studio when working with source generators. It does not give proper error messages. Instead, run
dotnet build
from a command line. You'll get much better error messages that way.In fact, part of my problem may be due to bugs or limitations in ReSharper (though I have not investigated this, so that is pure speculation).
Second, you can test the innards of your source generator without running the source generation at compile time.
OutputItemType="Analyzer"
in your project reference.Moreover, as NightOwl888 pointed out, I can add this to the referencing project in order to get it to spit out the generated files to disk (
as explained in this article):
I have not solved the IntelliSense problem, but now I can at least endure working with this generator.