【问题标题】:C# Source Generators debug in vscode在 vscode 中调试 C# 源代码生成器
【发布时间】:2021-04-23 09:25:26
【问题描述】:

如何在构建过程中使用 vscode 调试 ISourceGenerator 代码?网上的例子使用Debugger.Launch(),但这只适用于Visual Studio。

    [Generator]
    public class HelloWorldGenerator : ISourceGenerator
    {
        public void Execute(GeneratorExecutionContext context)
        {
      
        }

        public void Initialize(GeneratorInitializationContext context)
        {
            if (!Debugger.IsAttached)
            {
                // how to make it break here?
                Debugger.Launch();
            }
        }
    }

【问题讨论】:

  • 它没有回答您的问题,但您可以在受控环境中通过测试调试生成器cookbook
  • Debugger.Launch() 不是 Visual Studio 特定的;它将提供注册的任何调试器,所以我假设 VS Code 没有注册一个。我很少使用 VS Code,但 this 可能会有所帮助。
  • @sellotape 问题是vsdbg 在使用 vscode 的构建过程中没有运行。它可以在应用程序运行时正常调试。

标签: c# unix visual-studio-code msbuild roslyn


【解决方案1】:

如果你添加这样的构造:

while (!System.Diagnostics.Debugger.IsAttached)
    System.Threading.Thread.Sleep(500);

在附加任何所需的调试器之前,您将能够停止构建过程。我使用 Rider IDE,但这也适用于 VS Code。当它卡在循环中时,它可以作为 .NET Core 进程附加:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多