简体   繁体   English

Visual Studio(C#)中的条件编译

[英]Conditional Compilation in Visual Studio(C#)

I am attempting to make it so that we can have our application to behave differently based on the presence of a preprocessor directive. 我试图做到这一点,以使我们的应用程序可以基于预处理器指令的存在而表现不同。 I've read that you can create build configurations to define different preprocessor directives based on which build you do. 我读过您可以创建构建配置,以根据您进行的构建来定义不同的预处理程序指令。 Well, I am not seeing anything in Visual Studio to do this.. I know how to do it from the command line, but not how to do it within the automated environment of VS 2008. 好吧,我在Visual Studio中看不到要执行此操作的任何方法。我知道如何从命令行执行操作,但是不知道如何在VS 2008的自动化环境中执行操作。

Can someone tell me how to create a new build configuration which has preprocessor directives set in it? 有人可以告诉我如何创建一个设置了预处理程序指令的新构建配置吗?

Also, not sure if it has anything to do with it, but our project is an ASP.Net website 另外,不确定是否与此有关,但是我们的项目是ASP.Net网站

If you right-click the project and select "Properties", then select the Build tab, you can enter custom compilation symbols in "Conditional compilation symbols". 如果右键单击项目并选择“属性”,然后选择“生成”选项卡,则可以在“条件编译符号”中输入自定义编译符号。

For example, if your code looks like this: 例如,如果您的代码如下所示:

#if DEBUG
// do something
#else
// do something else
#end

You can set "DEBUG" as a Conditional compilation symbol. 您可以将“ DEBUG”设置为条件编译符号。

You can set different values for different build configurations, by changing the "Configuration" drop-down. 您可以通过更改“配置”下拉列表为不同的构建配置设置不同的值。

I do not even see a way to control the DEBUG symbol, and it is an ASP.Net website 我什至没有看到控制DEBUG符号的方法,它是一个ASP.Net网站

Ok, there's the problem. 好的,有问题。 The ASP.net web site option gives you a lot less control over builds, etc as it doesn't even use a project file. ASP.net网站选项使您对构建等的控制更少,因为它甚至不使用项目文件。

If it is at all feasible for you to switch, switch over to an ASP.net web application. 如果切换完全可行,请切换到ASP.net Web应用程序。 A web app will behave much more like a typical C# project (you have a project file, and the contents of the csproj file control what is in the app instead of just the directory structure, etc. Web应用程序的行为将更类似于典型的C#项目(您有一个项目文件,csproj文件的内容控制了应用程序中的内容,而不仅仅是目录结构等)。

After you have converted, you should see the options that you are expecting. 转换后,您应该看到期望的选项。

Here's a link with directions for converting: How To Convert ASP.NET Website to ASP.NET Web Application 这是转换方向的链接: 如何将ASP.NET网站转换为ASP.NET Web应用程序

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

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