How to target a different version of the framework in a different configuration of the same project?

I have a class library designed for .NET 3.5. Now I would like to add some functions that require .NET 4.0, but I still want to be able to generate a version intended for .NET 3.5 (without these functions, of course).

Ideally, I would like to target a different version of the framework based on the configuration:

  • Debugging, Release => .NET 3.5
  • Debugging 4.0, version 4.0 => .NET 4.0

Unfortunately, this is not possible, since the target structure is global for the entire project ...

Is there any way? I know that I can create a second project that includes the same files, but this is pretty bad for maintainability ....

What is the easiest way to do this? If you've ever done something like this, which approach worked best?

+4
source share
2 answers

First of all, you can start by creating a conditional compilation symbol that allows you to include or exclude additional functions that are available only for the .NET4.0 platform.

Also, I think you will have to use MSBuild directly, instead of letting VS.NET build your project. Once I did something similar. In short, it comes down to the fact that you will need to create 2 build tasks in your MSbuild script. One that allows you to create your own project for .NET 3.5, and one that allows you to create your own project for .NET 4.0.

In each build task, you can define the target structure and conditional compilation symbol that you want to use.

The build tasks in your build script might like this:

<Target Name="buildall-v4"> <!-- The following ItemGroup defines all the build-constants that have to be used in the build. As can be seen, the DEBUG & RELEASE constants are only included when necessary --> <ItemGroup> <BuildConstant Include="DEBUG" Condition="'$(buildmode)'=='DEBUG'" /> <BuildConstant Include="RELEASE" Condition="'$(buildmode)'=='RELEASE'" /> <BuildConstant Include="NET_FRAMEWORK_4_0" /> </ItemGroup> <PropertyGroup> <BuildConstantsToUse>@(BuildConstant)</BuildConstantsToUse> </PropertyGroup> <MSBuild Projects="$(builddir)\Source\MyProject.sln" Properties="OutputPath=$(outputdir)\v4;Configuration=$(buildmode);DefineConstants=$(BuildConstantsToUse);TargetFrameworkVersion=v4.0" /> </Target> <Target Name="buildall-v3.5"> <!-- The following ItemGroup defines all the build-constants that have to be used in the build. As can be seen, the DEBUG & RELEASE constants are only included when necessary --> <ItemGroup> <BuildConstant Include="DEBUG" Condition="'$(buildmode)'=='DEBUG'" /> <BuildConstant Include="RELEASE" Condition="'$(buildmode)'=='RELEASE'" /> </ItemGroup> <PropertyGroup> <BuildConstantsToUse>@(BuildConstant)</BuildConstantsToUse> </PropertyGroup> <MSBuild Projects="$(builddir)\Source\MyProject.sln" Properties="OutputPath=$(outputdir)\v3.5\;Configuration=$(buildmode);DefineConstants=$(BuildConstantsToUse);TargetFrameworkVersion=v3.5" /> </Target> 

Offcourse, when you want to build for two versions, you will have to run each msbuild command separately on the command line.

+4
source

I learned how to do this using:
http://blogs.msdn.com/b/visualstudio/archive/2010/05/14/a-guide-to-vcxproj-and-props-file-structure.aspx
and
http://shazwazza.com/post/multi-targeting-a-single-net-project-to-build-for-different-framework-versions/

Basically follow these steps:

  • Open the project file with a text editor so that you can view / edit all XML. Your project file is ProjectName.vcxproj.
    • Using a Visual Studio Text Editor
      • Unload the project you plan to edit. Right-click on the project you want to edit and select "Upload Project"; he will turn gray.
      • Use File-> Open-> File ... Ctrl + O and navigate to the location of the ProjectName.vcxproj file and open it. DO NOT USE File-> Open-> Project / Solution.
      • When you are done, you will want to save / close the file and right-click the project in the solution explorer and select "Update Project"
    • Or just use another editor, such as Notepad ++ or even a new instance of Visual Studio.
  • Further:

    • Delete

       <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> 
    • of

       <PropertyGroup Label="Globals"> 
    • and

       </PropertyGroup> 
      tags
    • .

  • Now:

    • Embed

       <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> 
    • or

       <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> 
    • as part of the corresponding "Debugging"

       <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> 
    • or "Debug 4.0"

       <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug 4.0|Win32'" Label="Configuration"> 
      • Since it is no longer in the Globals property group, make sure that each configuration has the <TargetFrameworkVersion Blah="blah blah"> in its PropertyGroup.
  • Save the changes (you may need to close the file if you use the same instance of visual studio where the project will be downloaded).

  • Download or restart the project in visual studio and make sure that individual configurations are built using different Framework.

+3
source

Source: https://habr.com/ru/post/1394712/