如何在 C# 中设置项目范围#define
我的项目中有几个类,它们只需要位于应用程序的某些版本中,而这些版本目前尚未准备好发布或调试。
为了防止使用这些类,我想在它们周围设置:
#if USE_MYCLASS
// Code here...
#endif
不幸的是,我不知道如何设置项目范围的#define。
Visual Studio 中是否有设置项目范围定义的功能?
如果有,尽管我现在不需要它,是否有设置解决方案范围定义的功能?
如果没有这样的功能(因为 C# 没有包含文件,我想这是可能的),是否有任何方法或插件可以在不使用命令行编译器和 /D 的情况下执行此功能?
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.
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?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在项目属性中执行此操作,但不能在源代码中执行此操作。
项目属性=>构建=>条件编译符号
您可以指定您需要的任何符号(空格分隔,但 IIRC 是相当宽容的)。请注意,
DEBUG
和TRACE
也可以通过复选框进行切换。我有一些具有多个“发布”构建配置的项目,每个项目都有不同的符号(用于构建 2.0、3.0 和 3.5 版本 - 请参阅
此处)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
DEBUG
andTRACE
can also be toggled with a checkbox.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)