visual-studio 如何检查在运行时使用 Visual Studio 构建的活动解决方案配置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31496/
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
How do I check the active solution configuration Visual Studio built with at runtime?
提问by Luke
I would like to enable/disable some code based on a custom solution configuration I added in Visual Studio. How do I check this value at runtime?
我想根据我在 Visual Studio 中添加的自定义解决方案配置启用/禁用一些代码。如何在运行时检查此值?
采纳答案by Jon Limjap
You can use precompiler directiveswithin Visual Studio. The #if directive will allow you to determine if you are going to include code or not based on your custom solution configuration.
您可以在 Visual Studio 中使用预编译器指令。#if 指令将允许您根据自定义解决方案配置确定是否要包含代码。
回答by CiNN
add a const value assign to a value that designate the configuration you are in. like
添加一个 const 值分配给一个值,指定您所在的配置。像
#ifdef _ENABLE_CODE1_
const codeconfig = 1;
#else
const codeconfig = 2;
#endif
and add _ENABLE_CODE1_ in your configuration preprocessor.
并在您的配置预处理器中添加 _ENABLE_CODE1_。
回答by Luke
In each project's properties under the build section you can set different custom constants for each solution configuration. This is where you define custom pre-compiler directives.
在构建部分下的每个项目的属性中,您可以为每个解决方案配置设置不同的自定义常量。这是您定义自定义预编译器指令的地方。
回答by Kibbee
I'm not sure if you can figure out the exact name of the build configuration. Howerver, if you use Debug.Assert(...), that code will only be run when you compile in debug mode. Not sure it that helps you at all.
我不确定您是否能找出构建配置的确切名称。但是,如果您使用 Debug.Assert(...),则该代码只会在您在调试模式下编译时运行。不确定它对你有帮助。

