c# - Visual Studio 忽略 #if DEBUG/RELEASE 范围内的代码,并且不检查错误或自动完成

标签 c# visual-studio-2017 visual-studio-2019 preprocessor-directive

我一直在编写 #if DEBUG#else#endif 代码片段,我注意到 Visual Studio 没有't let me use autocomplete to fulfill partially typed member names, and it does not check the grey out inactive code for errors.我发现让它再次受到关注的唯一方法是将构建模式从调试切换到发布。但这很不方便,感觉有更好的方法。

例子:

#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 的其他配置范围内的其他代码?

最佳答案

这是条件编译的目的,它按预期工作。使用条件编译应用程序可以在编译时忽略某些代码。您在 Visual Studio 中的应用程序在 Debug模式下运行,因此编译器忽略了 #else 部分中的代码。

Release 模式下运行您的应用程序,然后#else 代码将可用,但#if DEBUG 将不可用。

enter image description here

更新


要同时检查 #if DEBUG#else,您需要运行应用程序两次。

1.一旦进入调试模式,其中带有#if DEBUG的代码如下:

enter image description here enter image description here

此处应用程序处于 Debug模式,因此 #if DEBUG 条件代码处于事件状态..

  1. release 模式运行应用程序以检查#else 条件下的代码。在这里,其他部分也将能够使用自动完成和调试。

enter image description here enter image description here


有关这方面的更多信息,请参阅 Microsoft 文档:

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/debug-compiler-option

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/listed-by-category

关于c# - Visual Studio 忽略 #if DEBUG/RELEASE 范围内的代码,并且不检查错误或自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49610833/

相关文章:

c# - .NET 中的 DAL 和 BLL

c# - 从 VC++ 通过 COM 实例化 C# 对象的类未注册错误

visual-studio - 无法在没有互联网的计算机上更新 Visual Studio 2019?

git - 排除 Git 中受控文件的更改

c# - 从 ListView 中检索数据

c# - 如何为每个子类继承具有唯一值的静态属性?

c# - .NET Core 应用程序调试错误 : "Unable to set the next statement. This file does not exactly match the original version."

asp.net - Visual Studio 2017 ASP.Net 发布独立的 Dot Net Core 应用程序

visual-studio-2017 - 在还原nuget软件包时找不到本地本地的nuget本地错误

c++ - MSVC 19.22在Lambda上发出C4626警告