Changing the target application environment when changing build configurations in Visual Studio

I have these build configurations:

enter image description here

These platform configurations:

enter image description here

And these compiler conditions:

NET40 NET45 

My solution is a huge API that consists of 20 solutions, some of these solutions consume Async keywords and other benefits available only with .NetFx 4.5.

I have this part of the code in a conditional expression as follows:

 #If NET45 then Sub Async ... End Sub #Else Sub ... End Sub #End If 

Then what I'm trying to do, clearly, the .NetFx 4.5 assembly configurations should compile the NET45 conventions block, and the .NetFx 4.0 assembly configurations should compile the #Else block part.

The problem that I discovered is that if I change the target structure of the application in the project settings, this change is saved in all other build configurations, and I would like to avoid this resistance.

So how can I do this?


Note:

I tagged this question with a C # tag because it is a general Visual Studio environment issue, but I will clarify that my solutions are written in Vb.Net because I know that there are some big differences between the settings of a C # project, as well as their compiler , so maybe the C # extended answer couldn't help me.

+5
source share
2 answers

My suggestion is to get rid of conditional statements in the code by moving the platform / target / etc sencitive code in partial files. Then I will go to the project file and make the locked files sensitive to a certain condition, using all the fuctionality ms-build, which provide

Example:

  • Create a new VB Console application in Visual Studio
  • add three file classes ClassDotNetFeatures40.vb, ClassDotNetFeatures45.vb, GenericClass.vb
  • Add the following code

at GenericClass.vb

 Partial Public Class GenericClass Public Sub Hello() Console.Write("Hello ") End Sub End Class 

in ClassDotNetFeatures40.vb

 Partial Public Class GenericClass Public Sub Word() Console.Write("4.0 Word!") End Sub End Class 

in

ClassDotNetFeatures45.vb

 Public Class GenericClass Public Sub Word() Console.Write("4.5 Word!") End Sub End Class 
  • Put the following code in Module1.vb

     Sub Main() Dim o = New GenericClass() o.Hello() o.Word() End Sub 
  • Save all

  • Right-click your solution and click Upload Project.
  • Right-click the project file and click Edit Project.
  • Find the following lines:
 <Compile Include="ClassDotNetFeatures40.vb" /> <Compile Include="ClassDotNetFeatures45.vb" /> 

and replace them with

 <Compile Condition="'$(Configuration)' == 'Debug'" Include="ClassDotNetFeatures40.vb" /> <Compile Condition="'$(Configuration)' == 'Release'" Include="ClassDotNetFeatures45.vb" /> 
  • click save
  • right-click the project file and click Refresh

Now, when you start debugging the cancel project, you will get:

Hello 4.0 Word!

To cancel the release you will receive:

Hi 4.5 Word!

+2
source

You will need to modify the project files manually (I played with csproj - I hope vbproj works the same way).

All project configuration properties described in sections like this:

 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> ... </PropertyGroup> 

Pay attention to the Condition statement, which describes that this particular set of properties is specified for the Debug, AnyCPU configuration Debug, AnyCPU .

What you need to do is move the TargetFrameworkVersion property from the general top level to the configuration levels, something like this:

 <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> <PropertyGroup> <!-- general properties here - removing framework related... --> <!--<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>--> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <!-- Use 4.0 for Debug --> <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> <!-- other properties here... --> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <!-- Use 4.5 for Release --> <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> <!-- other properties here... --> </PropertyGroup> 

Please note that the VS.Net GUI does NOT support this and does not display the correct values ​​in the project properties window; although he will use these values ​​for assembly.

Depending on the complexity of your solution, you may find other artifacts, since VS.Net will not properly reload the project, but at least this should work with the build from the console.

In addition, you may need to use similar conditional β€œhacks” to link to the appropriate libraries.

+1
source

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


All Articles