C# 我可以阻止 CLR 优化掉调试信息吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1062714/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:19:29  来源:igfitidea点击:

Can I prevent the CLR from optimizing away debugging information?

c#.netoptimizationclrdebugging

提问by Rytmis

I've written an abstract base class for unit tests that sets up just enough environment for our tests to run. The class exposes some of the runtime environment bits as properties whose types vary test by test (the property types are type arguments specified in the inheriting, concrete test class).

我为单元测试编写了一个抽象基类,它为我们的测试运行设置了足够的环境。该类将一些运行时环境位公开为属性,其类型因测试而异(属性类型是在继承的具体测试类中指定的类型参数)。

This is all well and good, except a co-worker noticed that he can't view any of the class' properties in the debugger. Turns out the reason is that he had no fields defined in his inheriting class, and the CLR optimized something or other away, so the debugger couldn't display the properties. Is it possible to prevent this in the base class somehow, or do I have to resort to telling everyone they need to define at least one field which is used somewhere during the tests?

这一切都很好,除了一位同事注意到他无法在调试器中查看任何类的属性。原来原因是他的继承类中没有定义字段,并且 CLR 优化了某些东西或其他东西,因此调试器无法显示属性。是否有可能以某种方式在基类中防止这种情况,或者我是否必须告诉每个人他们需要定义至少一个在测试期间某处使用的字段?

Edit:

编辑:

Sounds like a likely culprit should be the optimization/debug settings. That said, I'm building the app from Visual Studio in Debug mode, I've double-checked that all projects are set for a debug build, and none of the projects in this solution have the Optimize flag set.

听起来像一个可能的罪魁祸首应该是优化/调试设置。也就是说,我正在调试模式下从 Visual Studio 构建应用程序,我已经仔细检查了所有项目是否都设置为调试版本,并且此解决方案中的所有项目都没有设置优化标志。

Perhaps it would also be relevant to note that I'm using MSTest and the Visual Studio test runner.

也许注意到我正在使用 MSTest 和 Visual Studio 测试运行器也很重要。

Edit 2:

编辑2:

By "can't view properties" I'm referring to when I evaluate the property in Quickwatch and get a red exclamation mark and a text "Could not evaluate expression" error text. And lest you think I'm entirely off base with my suspicions, adding an instance field that gets initialized in the test initialize method makes the problem go away...

“无法查看属性”是指当我在 Quickwatch 中评估属性并获得红色感叹号和文本“无法评估表达式”错误文本时。以免您认为我完全不相信我的怀疑,添加一个在测试初始化​​方法中初始化的实例字段会使问题消失......

Edit 3:

编辑3:

Checked the build output. I notice that the compiler is invoked with these options:

检查构建输出。我注意到编译器是用这些选项调用的:

/debug+
/debug:full
/optimize-
/define:DEBUG,TRACE

I should think that would be enough to stop this from happening, but there you go. :)

我应该认为这足以阻止这种情况发生,但是你去了。:)

采纳答案by Noldorin

I've encountered this same problem before, and it's invariably due to the fact that Debug mode has been turned off in some way. Try checking each of the following:

我以前也遇到过同样的问题,这总是由于调试模式已以某种方式关闭。尝试检查以下各项:

  1. The current build configuration for the solution and the appropiate project(s) is Debug.
  2. In the Buildtab of the property pages, the Optimize codecheckbox is unchecked.
  1. 解决方案和适当项目的当前构建配置是Debug
  2. 在属性页的构建选项卡中,优化代码复选框未选中

If this is all correct, then I recommend you paste the text written to the Outputwindow here so can we can potentially spot any more unusual cause of the issue.

如果这一切都正确,那么我建议您将写入“输出”窗口的文本粘贴到此处,以便我们可以潜在地发现问题的任何更不寻常的原因。

回答by phil soady

Dont Forget the obvious

不要忘记显而易见的

Make sure your arent trying to debug your release build. All of these compile settings set behind these configurations. The debug version is one for debugging ;-)

确保您没有尝试调试您的发布版本。所有这些编译设置都设置在这些配置后面。调试版本是用于调试的版本 ;-)

回答by Melvyn

In my case, the project configuration was correct, but my code was the result of a decompilation from ILSpy, and it had assembly attributes like the following:

在我的例子中,项目配置是正确的,但我的代码是从 ILSpy 反编译的结果,它具有如下的程序集属性:

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]

Removing these attributes fixed the debugger...

删除这些属性修复了调试器...