Conditionally changing target framework version

有些话、适合烂在心里 提交于 2020-01-30 06:02:05

问题


I'm trying to compile an #ifdef'd codebase to two different target frameworks, namely 3.5 and 4.0.

I attempted to modify the .proj files in the solution to no avail.

It seems MSBuild / VS2012 isn't picking up the solution configuration change via UI.

This is a fragment of one of the .proj files:

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>..\Binaries\</OutputPath>
    <DefineConstants>TRACE;NET35</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug 40|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>..\Binaries\</OutputPath>
    <DefineConstants>TRACE;DEBUG;NET40</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
  </PropertyGroup>

When I switch to "Debug 40" from "Release" the target framework does not change in the project properties (thus breaking the compilation because of other conditionally referenced assemblies).

I'm also having problems conditionally referencing different assemblies, as if, again, the solution configuration isn't picked up by VS/MSBuild (some don't even show up in the references).

Edit: I'm excluding Microsoft.CSharp from the v3.5 build with the following line:

<Reference Include="Microsoft.CSharp" Condition=" '$(Configuration)' == 'Debug 40'" />

So far it seems it's just the TargetFrameworkVersion property that is ignored.


回答1:


Turns out the problem was twofold:

  • ReSharper (v7) making things harder by displaying compilation errors across different files, making it harder to realize that code actually compiled with conditional references. I think version 7 does not support manual project file changes, so an heads up to RS7 users.
  • VS2012 not changing the target framework in the UI (despite actually using the correct framework when compiling, as seen in the verbose output of MSBuild).

I eventually managed to get the codebase to compile with a bit of patience. As per @granataCoder's suggestion, it's also better to keep different output paths (might be easy to overlook when dealing with orthogonal issues like conditional compilation).



来源:https://stackoverflow.com/questions/18588803/conditionally-changing-target-framework-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!