如何在C#中设置项目范围的#define [英] How to set project wide #define in C#
问题描述
我在一个项目中有几个类,这些类仅需要位于应用程序的某些特定版本中,而这些版本目前尚无法发布或调试.
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
不幸的是,我不知道如何在项目范围内设置#define.
Unfortunately, I don't know how to setup a project-wide #define.
Visual Studio中是否有用于设置项目范围定义的功能?
Is there functionality in Visual Studio to set project-wide definitions?
如果有(尽管我现在不需要),是否有功能来设置解决方案范围的定义?
If there is, though I don't need it right now, is there a functionality to set solution-wide definitions?
如果没有用于此功能的功能(我想这是可能的,因为C#没有包含文件),是否有任何方法或插件可以在不使用命令行编译器和/D的情况下执行此功能?
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?
推荐答案
您可以在项目属性中执行此操作,但不能在源代码中执行此操作.
You can do that in the project properties, but not in source code.
项目属性=>构建=>条件编译符号
Project Properties => Build => Conditional compilation symbols
您可以指定所需的任何符号(以空格分隔,但是IIRC相当宽容).请注意,DEBUG
和TRACE
也可以通过复选框进行切换.
You can specify whichever symbols you need (space delimited, but IIRC is is quite forgiving). Note that DEBUG
and TRACE
can also be toggled with a checkbox.
我有一些项目具有多个发布"构建配置,每个配置中都有不同的符号(用于构建2.0、3.0和3.5版本-请参见<DefineConstants>
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)
这篇关于如何在C#中设置项目范围的#define的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!