Visual Studio将忽略#if DEBUG/RELEASE范围内的代码,并且不检查错误或自动完成功能 [英] Visual Studio ignores the code inside #if DEBUG / RELEASE scope and doesn't check for errors or autocompletes
问题描述
我一直在编写 #if DEBUG
, #else
, #endif
的代码片段,我注意到Visual Studio并没有不要让我使用自动完成功能来填写部分键入的成员名称,并且它不会检查灰显的非活动代码中是否存在错误.我发现要再次注意的唯一方法是将构建模式从调试"切换为发布".但这很不方便,感觉有更好的方法.
I've been writing an #if DEBUG
, #else
, #endif
fragment of code, and I noticed that Visual Studio doesn't let me use autocomplete to fulfill partially typed member names, and it doesn't check the greyed out inactive code for errors. The only way I've found to make it care again is to switch the build mode from Debug to Release. But that's inconvenient and feels like there's a better way.
示例:
#if DEBUG
throw;
#else
throw new exc // I want to use autocomplete here but can't because it's greyed out
#endif
如何使VS停止忽略 #if DEBUG
的其他配置范围内的其他代码?
How do I make VS stop ignoring the other code inside the other configuration's scope of #if DEBUG
?
推荐答案
它是条件编译的目的,可以按预期运行.使用条件编译,应用程序可以在编译时忽略某些代码.Visual Studio中的应用程序正在调试模式下运行,因此编译器将忽略 #else
部分中的代码.
It is purpose of conditional compilation, it is working as intended. With conditional compilation application can ignore certain code at compilation. Your application in Visual Studio is running in Debug Mode so compiler is ignoring code inside #else
part.
以发布模式运行您的应用程序,则 #else
代码将可用,但 #if DEBUG
将不可用.
Run your application in Release mode then #else
code will be available but #if DEBUG
will not be available.
更新
用于同时检查 #if DEBUG
和& #else
,您需要运行两次应用程序.
For checking both #if DEBUG
& #else
you need to run application twice.
1.一旦进入 debug 模式,其中带有 #if DEBUG
的代码如下:
1.Once in debug mode in which code with #if DEBUG
like :
这里的应用程序处于调试模式,因此 #if DEBUG
条件代码处于活动状态..
here application is in debug mode so #if DEBUG
condition code is active..
- 以 release 模式运行应用程序,以检查
#else
条件下的代码.在这里,其他部分将可以使用自动填充和也进行调试.
- Run application in release mode for checking code in
#else
condition. Here other part will be able to use autocomplete & debug too.
有关此信息,请参考Microsoft文档:
Refer microsoft docs for more info on this:
这篇关于Visual Studio将忽略#if DEBUG/RELEASE范围内的代码,并且不检查错误或自动完成功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!