visual-studio 如何在 C# 中设置项目范围的#define
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1995869/
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 to set project wide #define in C#
提问by Brett Allen
I have several classes in a project which need to only be in certain builds of the application which are currently not ready for release or debug.
我在一个项目中有几个类,它们只需要在当前尚未准备好发布或调试的应用程序的某些构建中。
To prevent these classes from being used, I want to set around them this:
为了防止使用这些类,我想围绕它们设置:
#if USE_MYCLASS
// Code here...
#endif
Unfortunately, I don't know how to setup a project-wide #define.
不幸的是,我不知道如何设置项目范围的#define。
Is there functionality in Visual Studio to set project-wide definitions?
Visual Studio 中是否有设置项目范围定义的功能?
If there is, though I don't need it right now, is there a functionality to set solution-wide definitions?
如果有,虽然我现在不需要它,是否有设置解决方案范围定义的功能?
If there is no functionality for such (seeing as C# does not have include files, I suppose it's possible), is there any method or plugin of doing this functionality without using the command line compiler and /D?
如果没有这样的功能(因为 C# 没有包含文件,我想这是可能的),是否有任何方法或插件可以在不使用命令行编译器和 /D 的情况下执行此功能?
回答by Marc Gravell
You can do that in the project properties, but not in source code.
您可以在项目属性中执行此操作,但不能在源代码中执行此操作。
Project Properties => Build => Conditional compilation symbols
项目属性 => 构建 => 条件编译符号
You can specify whichever symbols you need (space delimited, but IIRC is is quite forgiving). Note that DEBUGand TRACEcan also be toggled with a checkbox.
您可以指定您需要的任何符号(空格分隔,但 IIRC 非常宽容)。请注意,DEBUG并TRACE也可以用一个复选框切换。
I have some projects with multiple "release" build configurations, with different symbols in each (for building 2.0 vs 3.0 vs 3.5 versions - see <DefineConstants>here)
我有一些具有多个“发布”构建配置的项目,每个项目都有不同的符号(用于构建 2.0 与 3.0 与 3.5 版本 - 请参见<DefineConstants>此处)

