visual studio 2010基于目标框架的编译条件

时间:2011-11-17 01:56:30

标签: c# visual-studio-2010 compiler-construction

我需要能够在我的代码中执行此操作:

#if NET_3_5
// .net 3.5 only code
#else
// non .net 3.5 code
#endif

基于this,我尝试将其添加到我的csproj文件中,但它没有帮助:

<DefineConstants Condition=" '$(TargetFrameworkVersion)' == 'v3.5' ">NET_3_5</DefineConstants>

更新

这是项目文件:

    <?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)' == '' ">AnyCPU</Platform>
    <ProductVersion>8.0.30703</ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>{FAA16900-38B9-4891-86C3-F595DA1FA6F7}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>RavenLinqpadDriver</RootNamespace>
    <AssemblyName>RavenLinqpadDriver</AssemblyName>
    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <TargetFrameworkProfile />
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <DefineConstants Condition=" '$(TargetFrameworkVersion)' == 'v3.5' ">NET_3_5</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup>
    <SignAssembly>true</SignAssembly>
  </PropertyGroup>
  <PropertyGroup>
    <AssemblyOriginatorKeyFile>RavenLinqpadDriverStrongNameKey.pfx</AssemblyOriginatorKeyFile>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="GalaSoft.MvvmLight">
      <HintPath>..\packages\MvvmLight.3.0.3\lib\net35\GalaSoft.MvvmLight.dll</HintPath>
    </Reference>
    <Reference Include="GalaSoft.MvvmLight.Extras">
      <HintPath>..\packages\MvvmLight.3.0.3\lib\net35\GalaSoft.MvvmLight.Extras.dll</HintPath>
    </Reference>
    <Reference Include="LINQPad">
      <HintPath>..\lib\LINQPad.exe</HintPath>
      <Private>False</Private>
    </Reference>
    <Reference Include="Newtonsoft.Json.Net35">
      <HintPath>..\lib\Newtonsoft.Json.Net35.dll</HintPath>
    </Reference>
    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />
    <Reference Include="Raven.Client.Lightweight-3.5">
      <HintPath>..\lib\Raven.Client.Lightweight-3.5.dll</HintPath>
    </Reference>
    <Reference Include="System" />
    <Reference Include="System.ComponentModel.DataAnnotations" />
    <Reference Include="System.Core" />
    <Reference Include="System.Windows.Interactivity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <HintPath>..\packages\MvvmLight.3.0.3\lib\net35\System.Windows.Interactivity.dll</HintPath>
    </Reference>
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="System.Data" />
    <Reference Include="System.Xml" />
    <Reference Include="WindowsBase" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="GuidValueConverter.cs" />
    <Compile Include="RavenDriver.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
    <Compile Include="RavenConectionDialog.xaml.cs">
      <DependentUpon>RavenConectionDialog.xaml</DependentUpon>
    </Compile>
    <Compile Include="RavenConnectionInfo.cs" />
    <Compile Include="RavenContext.cs" />
    <Compile Include="Utility.cs" />
  </ItemGroup>
  <ItemGroup>
    <None Include="packages.config" />
    <None Include="RavenLinqpadDriverStrongNameKey.pfx" />
  </ItemGroup>
  <ItemGroup>
    <Content Include="header.xml">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
  <ItemGroup>
    <Page Include="RavenConectionDialog.xaml">
      <SubType>Designer</SubType>
      <Generator>MSBuild:Compile</Generator>
    </Page>
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <PropertyGroup>
    <PostBuildEvent>
    </PostBuildEvent>
  </PropertyGroup>
  <!-- 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>

1 个答案:

答案 0 :(得分:4)

<DefineConstants>元素似乎只存在于Debug配置<PropertyGroup>下,这意味着它在构建版本时不会生效。最好的办法是将您的财产添加到单独的无条件属性组中。因此,在项目中第三个<PropertyGroup>元素关闭后,添加以下内容:

<PropertyGroup>
   <DefineConstants Condition=" '$(TargetFrameworkVersion)' == 'v3.5' ">$(DefineConstants);NET_3_5</DefineConstants> 
</PropertyGroup>

这将确保在定义新常量时保留调试/释放常量。