Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally changing target framework version

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.

like image 825
Raine Avatar asked Jul 26 '26 11:07

Raine


1 Answers

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).

like image 151
Raine Avatar answered Jul 28 '26 13:07

Raine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!