问题描述
我一直在编写 #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.
在 Release 模式下运行您的应用程序,然后 #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.Once 在 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:
https:///docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/debug-compiler-option
https:///docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/listed-by-category
这篇关于Visual Studio 忽略 #if DEBUG/RELEASE 范围内的代码,并且不检查错误或自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!