简体   繁体   English

如何在C#visual studio中定义预处理器符号

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

Sorry if my terminology is wrong. 对不起,如果我的术语错了。 I wrote #if TEST_APP in my code. 我在我的代码中写了#if TEST_APP Now i would like to define TEST_APP. 现在我想定义TEST_APP。 How do i set it using visual studios 2010? 我如何使用visual studios 2010进行设置? This is a windows form application. 这是一个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项目中设置的符号的名称,可以获得奖励

In visual studio solution explorer, right click on a project and click Properties. 在visual studio解决方案资源管理器中,右键单击项目,然后单击“属性”。 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. 常用符号还有2个复选框,DEBUG和TRACE。

For your web projects you could set the field to "WEB_PROJECT" and winforms to "WINFORMS_PROJECT" 对于您的Web项目,您可以将字段设置为“WEB_PROJECT”并将winforms设置为“WINFORMS_PROJECT”

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. 我不认为Web和winform应用程序默认定义了任何不同的符号。 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. 请记住,这是为项目本身设置的,并且不会影响任何类库 - 所以我希望项目中的任何代码都能真正知道它是否在Windows应用程序中,或者不在其中。 What were you thinking of using this for? 你在考虑用它做什么?

Method 1: 方法1:

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

Method 2: 方法2:

#define TEST_APP
#if defined(TEST_APP)
#endif

Source: MSDN 资料来源: MSDN

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 variable on StackOverflow. 如果您需要条件编译来动态反映构建或环境条件,请查看我如何在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: 我将展示如何根据环境条件启用条件编译,例如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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM