因此,this和this都很清楚。只需通过/p:DefineConstants="SYMBOL"
它对我来说根本不起作用,即使在测试项目中也是如此。我期望传递/ p:DefineConstants =“SYMBOL”将覆盖csproj中定义的任何条件编译常量。不是这样的......
以下完整代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DefineConstants
{
class Program
{
static void Main(string[] args)
{
#if DEV
Console.WriteLine("DEV");
#elif UAT
Console.WriteLine("UAT");
#else
Console.WriteLine("No environment provided");
#endif
}
}
}
.csproj文件是:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{57A2E870-0547-475C-B0EB-66CF9A2FE417}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DefineConstants</RootNamespace>
<AssemblyName>DefineConstants</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
使用:
构建C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild DefineConstants.sln /p:DefineConstants="DEV;DEBUG" /p:Configuration="Debug" /p:Platform="x86"
运行程序显示:
No environment provided
帮助!
答案 0 :(得分:29)
使用DefineConstants
绝对可行。这意味着你做错了什么。我的猜测是你首先构建项目而没有定义任何东西,然后重新构建它。然后,MSBuild将看到项目已经构建并且不会再次构建,而只是复制输出文件。您应该发布msbuild的输出,以便我们可以确定,但作为参考,我只使用必要的开关编译您的项目,这是结果(省略完整的msbuild输出):
> msbuild ConsoleApplication1.sln /p:DefineConstants="DEV" /t:Rebuild
....
Building solution configuration "Debug|x86".
Project ... is building ConsoleApplication1.csproj" (Rebuild target(s)).
...
> ConsoleApplication1\bin\Debug\DefineConstants.exe
DEV
> msbuild ConsoleApplication1.sln /p:DefineConstants="UAT" /t:Rebuild
...
> ConsoleApplication1\bin\Debug\DefineConstants.exe
UAT
> msbuild ConsoleApplication1.sln /t:Rebuild
...
> ConsoleApplication1\bin\Debug\DefineConstants.exe
No environment provided
答案 1 :(得分:0)
看来你没有传递条件编译符号。这就是为什么你输出的原因:没有提供环境。只需转到项目属性,然后单击构建选项卡(应用程序下方的左侧)。你会发现一个盒子要求条件编译符号。只需根据您想要的输出编写常量。对于前在你的例子中,打印&#34; DEV&#34;在条件编译框中编写DEV并重建项目。我相信它会奏效。
注意:如果您希望传递任何符号而不是根据您的代码,则会打印:未选择任何环境。