简体   繁体   English

在调试时隐藏DLL中的代码

[英]Hiding code from a DLL while debugging

I've created a DLL with a Class Library Project with some classes. 我已经创建了一个带有类库项目的DLL。 While adding this DLL as a reference in another projects and debugging, when going step by step or when the class returns an Exception the code from the class is shown. 将此DLL作为参考添加到另一个项目和调试时,当逐步执行或当类返回Exception时,将显示该类中的代码。

How can I hide this? 我怎么能隐藏这个? I want the exception to be shown on the class instruction, not inside and allowing to see al the code. 我希望异常显示在类指令上,而不是在内部,并允许查看代码。 And when debugging by steps, I want to do the methods without steping inside the code of the method. 并且当按步骤进行调试时,我想要在不执行方法代码的情况下执行这些方法。

Just like if you step through str.Split() , for example. 就像你通过str.Split() You don't see the code and all the steps. 您没有看到代码和所有步骤。 You just see the error on that line or jumps to the next one. 您只需在该行上看到错误或跳转到下一行。

For example: 例如:

Dim myObj As New myClass.SomeObj()

myObj.MyMethod()

I do not want the code inside MyMethod to be shown. 我不希望显示MyMethod的代码。

Add on the specified method a DebuggerStepThrough attribute to prevent step into. 在指定的方法上添加一个DebuggerStepThrough属性以防止步入。 If an exception occures, the debugger breaks at the method call, not inside the method. 如果发生异常,调试器会在方法调用中断开,而不是在方法内部。 See MSDN 请参阅MSDN

The behavior you describe is a convenience. 您描述的行为是一种便利。 It allows the caller to see exactly what is going wrong by looking at the details of the code he's trying to consume. 它允许调用者通过查看他尝试使用的代码的详细信息来查看确切的错误。 Microsoft even supports this for the .NET Framework source , and it's rather useful in my opinion. Microsoft甚至支持.NET Framework源代码 ,在我看来它非常有用。 I'm not really sure why you'd want to disable it. 我不确定你为什么要禁用它。 You can always just use F10 (Step Over) instead of F11 (Step Into) when debugging so that the DLL's code remains available in case you ever need it. 在调试时,您总是可以使用F10 (Step Over)而不是F11 (Step Into),这样DLL的代码在您需要时仍然可用。

But if you are sure that you don't want to be able to step into any code from the DLL, you need to make sure that the debug symbols are not available to the client application. 但是,如果您确定不希望能够从DLL中插入任何代码,则需要确保客户端应用程序无法使用调试符号。 Visual Studio generates these symbols in the form of a PDB file , which contains the location of the source files and mappings between the generated code and the source lines. Visual Studio 以PDB文件的形式生成这些符号,该文件包含源文件的位置以及生成的代码和源代码行之间的映射。

Contrary to some of the other answers, the generation of debug symbols is unrelated to whether the code is optimized (eg, a "Release" build). 与一些其他答案相反,调试符号的生成与代码是否被优化无关(例如,“发布”构建)。 I've written about this before in the context of why you might want symbols for an optimized build, but the point is that these are two orthogonal settings. 我之前在为什么你可能想要优化构建的符号的上下文中写过这个 ,但关键是这些是两个正交设置。 You can turn on optimization and turn off symbol generation, and vice versa. 您可以打开优化并关闭符号生成,反之亦然。 Suffice it to say that I strongly recommend generating debug symbols for all builds. 我只想说我强烈建议为所有版本生成调试符号。

You can disable the generation of debug symbols in the project's properties (it's hidden under the "Advanced..." button), or you can just move the PDB files to ensure that the client application cannot locate them when debugging. 您可以在项目属性中禁用调试符号的生成(它隐藏在“高级...”按钮下),或者您可以移动PDB文件以确保客户端应用程序在调试时无法找到它们。 By default, a build places them into the same directory as the binary output, so that when you add a reference to the DLL, Visual Studio finds them easily. 默认情况下,构建将它们放在与二进制输出相同的目录中,以便在添加对DLL的引用时,Visual Studio可以轻松地找到它们。 If you move either the symbols or the binaries, it won't be able to find them. 如果移动符号或二进制文件,它将无法找到它们。 (The debugger also searches the symbol path, but your symbols probably won't end up there.) (调试器也会搜索符号路径,但您的符号可能不会在那里结束。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM