26

したがって、これこれはどちらもかなり明確です。単に渡す/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

ヘルプ!

4

2 に答える 2

32

使用する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 
于 2012-05-23T08:13:02.387 に答える
-1

条件付きコンパイルシンボルを渡していないようです。そのため、次のように出力されます。環境が提供されていません。プロジェクトのプロパティに移動し、ビルドタブ(アプリケーションの下の左側)をクリックするだけです。条件付きコンパイルシンボルを要求する1つのボックスがあります。希望する出力に従って定数を書き込むだけです。例:あなたの例では、「DEV」を印刷するには、条件付きコンパイルボックスにDEVを書き込み、プロジェクトを再構築します。私はそれがうまくいくと確信しています。

注:コードよりもシンボルを渡したい場合は、次のように出力されます:環境が選択されていません。

于 2017-08-01T05:35:50.960 に答える