2

I would like to have two different .net framework targets via configuration in Visual Studio 2015. While for references, you can edit the CSPROJ file and add a conditions, this does not seem to work for the TargetFrameworkVersion in the first PropertyGroup of the file. I have the impression that any Condition in that element causes VS to completely ignore this element and to fall back to the default value of "v4.0".

Is there any way I can get different target framework versions for different configurations?

This is what I tried in the CSPROJ file:

<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    ...
    <!-- this is what VS2015 would put into the file:
    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
    -->
    <!-- this is what does not work: -->
    <TargetFrameworkVersion Condition="'$(Configuration)' == 'OLD_Debug' OR '$(Configuration)' == 'OLD_Release'">v3.5</TargetFrameworkVersion>
    <TargetFrameworkVersion Condition="'$(Configuration)' == 'NEW_Debug' OR '$(Configuration)' == 'NEW_Release'">v4.0</TargetFrameworkVersion>
    ...
  </PropertyGroup>
  ...
</Project>

A similar approach with conditions for the assembly references works fine.

EDIT I found a similar Stackoverflow question: Targetting multiple .net framework versions by using different project configurations and tried the approach suggested in the non-accepted answer to remove the TargetFrameworkVersion from the first PropertyGroup block, and edit the later <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'OLD_Debug|AnyCPU' "> blocks to contain it, but my assembly is still compiled for framework 3.5 no matter which configuration I use. At least if I look at the assembly from Powershell using [System.Reflection.Assembly]::LoadFrom("C:\PATH\MyAssembly.dll").ImageRuntimeVersion, I always get version 2, not 4.

1
  • I did this before by having a build script pass the TargetFrameworkVersion as a parameter to MSBuild for each. Do note this can be accomplished far easier in Visual Studio 2017 with the new .csproj format: <TargetFrameworks>net35;net45;netstandard2.0</TargetFrameworks>, which builds all versions in Visual Studio. Commented Feb 12, 2018 at 12:26

1 Answer 1

3

The approach found in this answer to a similar question works: Keep the first PropertyGroup without configuration specific settings, remove the TargetFrameworkVersion element from it. And add the TargetFrameworkVersion settings to the configuration specific PropertyGroups which are in the file anyway, just double them for debug/release:

<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    ...
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'OLD_Debug|AnyCPU' ">
    ...
    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'OLD_Release|AnyCPU' ">
    ...
    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'NEW_Debug|AnyCPU' ">
    ...
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'NEW_Release|AnyCPU' ">
    ...
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
  </PropertyGroup>
  ...
</Project>

I verified this as follows:

  1. My assembly references version 2.0.0.0 of the mscorlib assembly for the 3.5 framework (OLD_... configurations), and version 4.0.0.0 of mscorlib for the 4.0 framework (NEW_... configurations).
  2. Using ILSpy, I found that the 3.5 version of my assembly has no attribute for the target framework, as this was only introduced since version 4, but the version 4 framework appears as an attribute of the assembly:

    [assembly: TargetFramework(".NETFramework,Version=v4.0", FrameworkDisplayName = ".NET Framework 4")]
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.