简体   繁体   English

如何将数据传递给 csproj,或者我可以在 C# .netFramework 项目中的新构建配置上使用#if 指令

[英]How can I pass data to csproj or can I use #if directive on a new build configuration in C# .netFramework project

I want to create conditional compilation of a .NET framework web forms project because I want to have to build the project in two different ways.我想创建 .NET 框架 web forms 项目的条件编译,因为我想以两种不同的方式构建项目。 I wanted to create something like this:我想创建这样的东西:

#if X
    Console.WriteLine("executeCodeX");
#else
    Console.WriteLine("executeCodeY");
#endif

where X should be defined in the msbuild file as a property so I can have the option to build as X or build as Y. I thought of the following two solutions: 1) create a new build configuration for example buildX, and in msbuild call the msbuild command using buildX configration.其中 X 应在 msbuild 文件中定义为属性,因此我可以选择构建为 X 或构建为 Y。我想到了以下两种解决方案:1)创建一个新的构建配置,例如 buildX,并在 msbuild 调用使用 buildX 配置的 msbuild 命令。 The problem with this solution is that I don't know how inside of my code I can know what configuration I'm executing with (I can't define the #if X, since X is not recognized).这个解决方案的问题是我不知道我的代码内部如何知道我正在执行什么配置(我无法定义#if X,因为 X 无法识别)。

2) create a property in the msbuild and pass it to the csproj in some way, but yet again, I don't know how to access this property inside of my code in order to tell if I'm executing in X or not. 2)在 msbuild 中创建一个属性,并以某种方式将其传递给 csproj,但是我不知道如何在我的代码中访问这个属性,以判断我是否在 X 中执行。

what is the right approach for the solution?解决方案的正确方法是什么?

You need to use the MSBuild property "DefineConstants"您需要使用 MSBuild 属性“DefineConstants”
You could set/modify it in a csproj (single project) file or a Directory.Build.props file (whole solution), or just specify it as a command line parameter.您可以在 csproj(单个项目)文件或 Directory.Build.props 文件(整个解决方案)中设置/修改它,或者仅将其指定为命令行参数。 It depends exactly how you want things to work.这完全取决于您希望事情如何工作。

If you want to specify it in a proj/props file try putting something like this just before the project tag at the end of the project tag:如果您想在 proj/props 文件中指定它,请尝试在项目标签末尾的项目标签之前放置类似这样的内容:
Using a proxy property makes it easier to add more defined constants, switch what the defaults are, and toggle them on and off separately in the future.使用代理属性可以更轻松地添加更多定义的常量、切换默认值以及将来单独打开和关闭它们。

<PropertyGroup>
    <UseX>false</UseX>
    <DefineConstants Condition="'$(UseX)' == 'true'">X;$(DefineConstants)</DefineConstants>
</PropertyGroup>

The <UseX>false</UseX> isn't strictly necessary, but feels slightly more complete. <UseX>false</UseX>不是绝对必要的,但感觉更完整。

Then when you want to build it use the following option to build with X off:然后,当您要构建它时,请使用以下选项在关闭 X 的情况下构建:

msbuild <proj/sln file>  

And with X on:并打开 X:

msbuild <proj/sln file> /p:UseX=true  

Alternatively, the simplest option with little flexibility is to just specify this on the command line:或者,最简单且灵活性很小的选项是仅在命令行上指定:

msbuild <proj/sln file> /p:DefineConstants=X 

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

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