简体   繁体   English

基于目标框架的Visual Studio 2010编译器条件

[英]visual studio 2010 compiler conditions based on target framework

I need to be able to do this in my code: 我需要能够在我的代码中执行此操作:

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

Based on this , I tried adding this to my csproj file, but it didn't help: 基于 ,我尝试将其添加到csproj文件中,但没有帮助:

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

update 更新

Here's the project file: 这是项目文件:

    <?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>

It appears that the <DefineConstants> element exists only under the Debug configuration <PropertyGroup> , which means that it would not take effect when you are building release. 看来<DefineConstants>元素仅在Debug配置<PropertyGroup>下存在,这意味着在构建发行版时它不会生效。 The best thing to do would be to add your property in a separate unconditional property group. 最好的办法是将您的财产添加到一个单独的无条件财产组中。 So, after the closing of the third <PropertyGroup> element in your project, add the following: 因此,在项目中的第三个<PropertyGroup>元素关闭之后,添加以下内容:

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

This will ensure that the debug/release constants are preserved while your new constant is defined. 这将确保在定义新常量时保留调试/释放常量。

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

相关问题 Visual Studio 2010 +目标框架3.5。 将使用什么版本的c#编译器? - Visual Studio 2010 + Target Framework 3.5. What version of c# compiler will be used? 在Visual Studio 2010中更改目标框架 - Changing Target Framework in Visual Studio 2010 错误:Microsoft Visual Studio 2010上的“ .Net Framework许可证编译器” devexpress - Error: “.Net Framework license compiler” devexpress on Microsoft Visual Studio 2010 这是Visual Studio 2010编译器中的错误吗? - Is this a bug in Visual Studio 2010 compiler? Visual Studio中的“目标框架” - “Target Framework” in Visual Studio 在Visual Studio 2012中为Office 2010构建VSTO项目时,出现“未安装Project Target Framework”错误 - “Project Target Framework Not installed” error comes when building the VSTO project for Office 2010 in Visual studio 2012 Visual Studio 2010:无法解析目标框架“.NETFramework,Version=v4.0”的 mscorlib - Visual Studio 2010: Could not resolve mscorlib for target framework '.NETFramework,Version=v4.0' 在Visual Studio命令提示符2010中构建c#dll时如何指定.net目标框架? - How to specify .net target framework when building c# dll in Visual Studio Command Prompt 2010? 降级Visual Studio 2010的实体框架 - Downgrading Entity Framework for visual studio 2010 Entity Framework 5中的Visual Studio 2010是否支持LocalDB? - Is LocalDB supported by Visual Studio 2010 in Entity Framework 5?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM