.net 如何在 Visual Studio 中的引用代码中设置断点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2617659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I set a breakpoint in referenced code in Visual Studio?
提问by Dabblernl
My main solution is using code from a utility class library, that I wrote myself, but is a part from another solution. How can I set a breakpoint in the referenced DLL file?
我的主要解决方案是使用我自己编写的实用程序类库中的代码,但它是另一个解决方案的一部分。如何在引用的 DLL 文件中设置断点?
回答by SLaks
Click Debug, New Breakpoint, Break at Function, then enter the full name of the function.
单击“调试”、“新建断点”、“在函数处中断”,然后输入函数的全名。
回答by Olivier Jacot-Descombes
In Visual Studio open the source file of your referenced DLL that contains the desired method manually using menu
在 Visual Studio 中,使用菜单手动打开包含所需方法的引用 DLL 的源文件
File > Open > File...
文件 > 打开 > 文件...
Then set the breakpoint by clicking on the left border in the code editor. This enables you to break at any code line and not just at function calls. Visual Studio shows the breakpoint in a kind of disabled state, because it thinks that the code is unreachable. Just ignore it; the breakpoint will become active once the code runs and the DLL has been loaded.
然后通过单击代码编辑器中的左边框设置断点。这使您可以在任何代码行中断,而不仅仅是在函数调用处中断。Visual Studio 以一种禁用状态显示断点,因为它认为代码无法访问。忽略它;一旦代码运行并加载了 DLL,断点将变为活动状态。
Note: you must reference a Debug version of your assembly for this to work.
注意:您必须引用程序集的调试版本才能使其工作。
回答by Josh
You can do one of the following:
您可以执行以下操作之一:
- Add the DLL project to the solution containing your executable. Then you can set breakpoints as normal.
- You could instead just open the DLL project and use the Debug -> Attach to Process to attach to your running EXE
- 将 DLL 项目添加到包含可执行文件的解决方案中。然后你可以正常设置断点。
- 您可以改为打开 DLL 项目并使用 Debug -> Attach to Process 附加到您正在运行的 EXE
回答by Hellraiser
I know this is an old question, but may be of help to many.
我知道这是一个老问题,但可能对许多人有帮助。
For the debugger to work correctly, you need to load debugging symbols database, a .pdb file with the same name as the assembly you want to debug. If it's part of a solution you created you could just copy-paste it from the other solution's bin folder. Then add a breakpoint specifying the full path to the method you want to debug, plus the name of the assembly it lives in. EX: "MyNamespace.MayClass.MyMethod, MyAssemblyName"
要使调试器正常工作,您需要加载调试符号数据库,这是一个与要调试的程序集同名的 .pdb 文件。如果它是您创建的解决方案的一部分,您只需从其他解决方案的 bin 文件夹中复制粘贴即可。然后添加一个断点,指定要调试的方法的完整路径,以及它所在的程序集的名称。例如:“MyNamespace.MayClass.MyMethod, MyAssemblyName”
If you don't own the code you have 2 options, both involving a dissasembler. I use dotPeek for this, since it really rocks.
如果您不拥有代码,则有 2 个选项,均涉及反汇编程序。我为此使用 dotPeek,因为它真的很棒。
Option 1: you open the assembly with dotPeek and create a single .pdb for that, then copy it to your .bin folder and follow the steps above. https://www.jetbrains.com/decompiler/help/Generating_PDB_Files.html
选项 1:您使用 dotPeek 打开程序集并为此创建一个 .pdb,然后将其复制到您的 .bin 文件夹并按照上述步骤操作。https://www.jetbrains.com/decompiler/help/Generating_PDB_Files.html
Option 2: use dotPeek Symbol Server and PDB Generation. https://www.jetbrains.com/decompiler/help/Symbol_Server_and_PDB_Generation.htmlAfter that follow the instructions above to attach a debugger instance.
选项 2:使用 dotPeek Symbol Server 和 PDB Generation。 https://www.jetbrains.com/decompiler/help/Symbol_Server_and_PDB_Generation.html然后按照上面的说明附加调试器实例。
Hope this helps
希望这可以帮助
回答by Matt Dearing
Make sure you have the .pdb file in the bin/debug folder where the referenced class library dll resides. When you are debugging your current solution you should be able to step into the code from your class library. When you step into the class library you will be able to set breakpoints.
确保引用的类库 dll 所在的 bin/debug 文件夹中有 .pdb 文件。当您调试当前的解决方案时,您应该能够从您的类库进入代码。当您进入类库时,您将能够设置断点。
回答by Vahid Farahmandian
follow these steps:
按着这些次序:
- Go to
Debug - Go to
New Breakpoint - Click on
Function Breakpointor simple press theCtrl+K, B - a window shows up, type the function name in the following format:
- 去
Debug - 去
New Breakpoint - 单击
Function Breakpoint或简单按Ctrl+K, B - 出现一个窗口,按以下格式键入函数名称:
namespace.ClassName.FunctionName
命名空间.ClassName.FunctionName
For example, assume that you have a code like this and I want to put a breakpoint at the beginning of function D:
例如,假设您有这样的代码,并且我想在函数的开头放置一个断点D:
namespace A.B{
public class C{
public void D(){
int x= 10;
}
}
}
So in Function Breakpointwindow you need to type: A.B.C.D
所以在Function Breakpoint窗口中你需要输入:A.B.C.D
回答by makoshichi
This is not my own answer, it was Frep D-Oronge's suggestion in one of the comments above. It is easy and works with no hiccups:
这不是我自己的答案,而是 Frep D-Oronge 在上述评论之一中的建议。它很容易并且没有任何问题:
"I find easy - just run two instances of Studio side by side. Ctrl-F5 on the 'primary' one to launch without the debugger attached, then attach to the process with the instance of studio that is editing the library project"
“我觉得很简单 - 只需并排运行 Studio 的两个实例。在'主要'一个实例上按 Ctrl-F5 即可在不附加调试器的情况下启动,然后使用正在编辑库项目的 Studio 实例附加到进程中”
All credits are due to him.
所有的功劳都归功于他。

