visual-studio 我如何在 C# 视觉工作室中定义预处理器符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2355340/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:36:24  来源:igfitidea点击:

How do i define a preprocessor symbols in C# visual studios

c#visual-studiovisual-studio-2010

提问by Russell

Sorry if my terminology is wrong. I wrote #if TEST_APPin my code. Now i would like to define TEST_APP. How do i set it using visual studios 2010? This is a windows form application.

对不起,如果我的术语是错误的。我写#if TEST_APP在我的代码中。现在我想定义 TEST_APP。我如何使用 Visual Studios 2010 设置它?这是一个 windows 窗体应用程序。

Bonus if you can tell me the name of the symbol that is set in a winform project and in a web project

如果你能告诉我在 winform 项目和 web 项目中设置的符号的名称,则奖励

回答by Russell

In visual studio solution explorer, right click on a project and click Properties. Open the build tab and you will see a field "Conditional compilation symbols". This is a comma separated list, or space separated. There are also 2 checkboxes for commonly used symbols, DEBUG and TRACE.

在 Visual Studio 解决方案资源管理器中,右键单击一个项目,然后单击“属性”。打开构建选项卡,您将看到一个字段“条件编译符号”。这是一个逗号分隔的列表,或空格分隔。还有 2 个用于常用符号的复选框,DEBUG 和 TRACE。

For your web projects you could set the field to "WEB_PROJECT" and winforms to "WINFORMS_PROJECT"

对于您的网络项目,您可以将该字段设置为“WEB_PROJECT”并将 winforms 设置为“WINFORMS_PROJECT”

回答by Jon Skeet

In the Build tab of the properties page for the project, look for the "Conditional compilation symbols" setting.

在项目属性页面的构建选项卡中,查找“条件编译符号”设置。

I don't believe there are any different symbols defined by default for web and winform applications. Bear in mind this is set for the project itself, and won't affect any class libraries - so I'd expect any code within a project to really know whether it's in a Windows application or not to start with. What were you thinking of using this for?

我不相信默认情况下为 web 和 winform 应用程序定义了任何不同的符号。请记住,这是为项目本身设置的,不会影响任何类库 - 所以我希望项目中的任何代码真正知道它是否在 Windows 应用程序中。你想用这个做什么?

回答by Michal Ciechan

Method 1:

方法一:

#define TEST_APP true
#if TEST_APP == true
#endif

Method 2:

方法二:

#define TEST_APP
#if defined(TEST_APP)
#endif

Source: MSDN

来源:MSDN

回答by Glenn Slayden

If you need your conditional compilation to dynamically reflect the build or environment conditions, check my answer to How do I set a conditional compile variableon StackOverflow. I show how to enable conditional compilation based on ambient conditions, such as C# language version, so that you can write code like this:

如果您需要条件编译来动态反映构建或环境条件,请查看我对如何在 StackOverflow 上设置条件编译变量的回答。我展示了如何根据环境条件启用条件编译,例如C# 语言版本,以便您可以编写如下代码:

#if CSHARP7
    ref T pi = ref rg[i], pj = ref rg[j];
    var t = pi;                    // swap elements: managed pointers
    pi = pj;
    pj = t;
#else
    var t = rg[i];                 // swap elements: clunky
    rg[i] = rg[j];
    rg[j] = t;
#endif