使用源代码将进程附加到调试器?

Attach process to debugger with source code?

虽然 运行 一个处于调试模式的 C# 控制台应用程序,是否可以启动另一个 C# 控制台应用程序(相同的程序或不同的程序),使用 Process.Start(),并且然后使用 C# 源代码将其自动附加到调试器?

我正在使用 Visual Studio 2017+、.NET 4.7.2、C# 7.3。

此问题不重复,因为 questions/answers 中建议的 none 完全回答了此特定问题。我正在寻找一种使用源代码将任何进程对象附加到当前调试器的全自动方式。 None 其他问题的答案专门针对此问题。例如,他们建议使用过时的插件、生成新的调试器实例或手动附加进程,而不是使用 C# 源代码完成任务。

例如:

var this_exe = Environment.GetCommandLineArgs()[0];
var p = Process.Start(this_exe, "/compute");
// How to attach 'p' to the debugger?

在您使用Process启动的exe的Main方法中,添加Debugger.Launch(),如

private static void Main(string[] args)
{
    Debugger.Launch();
    //your code
}