在Visual Studio中更改构建配置时更改应用程序目标框架

时间:2023-01-14 22:58:12

I have these build configurations:

我有这些构建配置:

在Visual Studio中更改构建配置时更改应用程序目标框架

These platform configurations:

这些平台的配置:

在Visual Studio中更改构建配置时更改应用程序目标框架

And these compiler conditionals:

这些编译器条件:

NET40
NET45

My solution is a huge API that consists in 20 solutions, some of those solutions consumes Async keywords and other beneffits that are available only from .NetFx 4.5.

我的解决方案是一个包含20个解决方案的大型API,其中一些解决方案使用的是异步关键字,以及其他只能从. netfx 4.5获得的优点。

That part of the code I have it in a conditional in this way:

这部分代码我有一个条件,以这种方式:

#If NET45 then

    Sub Async
    ...
    End Sub

#Else

    Sub
    ...
    End Sub

#End If

Then, what I'm trying to do is clear, the .NetFx 4.5 build configurations should compile the block of the NET45 conditional, and the .NetFx 4.0 build configurations should compile the block of the #Else part.

然后,我要做的很清楚,. netfx 4.5构建配置应该编译NET45条件块,而. netfx 4.0构建配置应该编译#Else部分的块。

The problem I found is that if I change the application target framework in the project settings, the change persist in all the other build configurations, and I would like to avoid that persistance.

我发现的问题是,如果我在项目设置中更改应用程序目标框架,那么更改将在所有其他构建配置中持续存在,我希望避免这种持久性。

So how I can do this?.

我怎么做呢?


Note:

注意:

I marked this question with the C# tag because is a general Visual Studio environment question, but I will clarify that my solutions are written in Vb.Net, because I know there are some big differences between the C# project settings and also their compiler params so maybe a C# advanced answer could not help me.

我用c#标记了这个问题,因为这是一个通用的Visual Studio环境问题,但是我将澄清我的解决方案是用Vb编写的。Net,因为我知道c#项目设置和它们的编译器解析之间有很大的不同,所以c#高级答案可能对我没有帮助。

2 个解决方案

#1


2  

My suggestion is to get rid of conditional statements in code by moving platform/target/etc sencitive code in partial files. Then I would go to project file and would make the icluded files sensitive on particular condition using all the fuctionality ms-build provides

我的建议是,在部分文件中,通过移动平台/目标/等sencitive代码,去掉代码中的条件语句。然后,我将进入项目文件,使用所有的ms-build提供的fuc合度,使icluded文件在特定条件下变得敏感

Example:

例子:

  • Create brand new VB Console App in Visual Studio
  • 在Visual Studio中创建全新的VB控制台应用
  • add three class files ClassDotNetFeatures40.vb, ClassDotNetFeatures45.vb, GenericClass.vb
  • 添加三个类文件ClassDotNetFeatures40。vb,ClassDotNetFeatures45。vb,GenericClass.vb
  • Add the following code
  • 添加以下代码

in GenericClass.vb

在GenericClass.vb

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

in ClassDotNetFeatures40.vb

在ClassDotNetFeatures40.vb

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

in

ClassDotNetFeatures45.vb

ClassDotNetFeatures45.vb

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

    将以下代码放在Module1.vb中

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

    保存所有

  • Right click on your solution and press Unload Project
  • 右键单击您的解决方案并按卸载项目
  • Right click on the project file and press Edit Project
  • 右键单击项目文件并按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" />
  • press save
  • 按保存
  • right click on project file and press Reload
  • 右键单击项目文件并按Reload

now when you run the project undo debug you will get:

现在,当您运行项目撤消调试时,您将得到:

Hello 4.0 Word!

你好4.0字!

undo release you willl get:

撤消释放你将得到:

Hello 4.5 Word!

你好4.5字!

#2


1  

You will need to change project files manually (I've played with csproj - hopefully vbproj works in the same way).

您将需要手动更改项目文件(我已经和csproj一起玩了,希望vbproj也能以同样的方式工作)。

All project configurations properties described in the sections like this one:

所有项目配置属性,如本文所述:

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

Please notice Condition statement - that describes that this particular property set specified for Debug, AnyCPU configuration.

请注意条件语句——它描述了为Debug(任何cpu配置)指定的这个特定属性集。

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

您需要做的是将TargetFrameworkVersion属性从一般的顶层移动到配置特定的级别,如下所示:

<?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 notice that VS.Net GUI does NOT support this, and will not display correct values in the project Properties window; though it will use these values for build.

请注意,VS.Net GUI不支持这一点,并且不会在项目属性窗口中显示正确的值;尽管它将使用这些值来构建。

Depending on complexity of your solution, you might found some other artifacts, as VS.Net will not reload project properly, but at least that should work with build from console.

根据您的解决方案的复杂性,您可能会发现一些其他的工件,因为VS.Net不能正确地重载项目,但是至少应该使用控制台生成。

Additionally, you might need to use similar conditional "hacks" to reference correct libraries.

此外,您可能需要使用类似的条件“hack”来引用正确的库。

#1


2  

My suggestion is to get rid of conditional statements in code by moving platform/target/etc sencitive code in partial files. Then I would go to project file and would make the icluded files sensitive on particular condition using all the fuctionality ms-build provides

我的建议是,在部分文件中,通过移动平台/目标/等sencitive代码,去掉代码中的条件语句。然后,我将进入项目文件,使用所有的ms-build提供的fuc合度,使icluded文件在特定条件下变得敏感

Example:

例子:

  • Create brand new VB Console App in Visual Studio
  • 在Visual Studio中创建全新的VB控制台应用
  • add three class files ClassDotNetFeatures40.vb, ClassDotNetFeatures45.vb, GenericClass.vb
  • 添加三个类文件ClassDotNetFeatures40。vb,ClassDotNetFeatures45。vb,GenericClass.vb
  • Add the following code
  • 添加以下代码

in GenericClass.vb

在GenericClass.vb

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

in ClassDotNetFeatures40.vb

在ClassDotNetFeatures40.vb

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

in

ClassDotNetFeatures45.vb

ClassDotNetFeatures45.vb

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

    将以下代码放在Module1.vb中

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

    保存所有

  • Right click on your solution and press Unload Project
  • 右键单击您的解决方案并按卸载项目
  • Right click on the project file and press Edit Project
  • 右键单击项目文件并按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" />
  • press save
  • 按保存
  • right click on project file and press Reload
  • 右键单击项目文件并按Reload

now when you run the project undo debug you will get:

现在,当您运行项目撤消调试时,您将得到:

Hello 4.0 Word!

你好4.0字!

undo release you willl get:

撤消释放你将得到:

Hello 4.5 Word!

你好4.5字!

#2


1  

You will need to change project files manually (I've played with csproj - hopefully vbproj works in the same way).

您将需要手动更改项目文件(我已经和csproj一起玩了,希望vbproj也能以同样的方式工作)。

All project configurations properties described in the sections like this one:

所有项目配置属性,如本文所述:

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

Please notice Condition statement - that describes that this particular property set specified for Debug, AnyCPU configuration.

请注意条件语句——它描述了为Debug(任何cpu配置)指定的这个特定属性集。

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

您需要做的是将TargetFrameworkVersion属性从一般的顶层移动到配置特定的级别,如下所示:

<?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 notice that VS.Net GUI does NOT support this, and will not display correct values in the project Properties window; though it will use these values for build.

请注意,VS.Net GUI不支持这一点,并且不会在项目属性窗口中显示正确的值;尽管它将使用这些值来构建。

Depending on complexity of your solution, you might found some other artifacts, as VS.Net will not reload project properly, but at least that should work with build from console.

根据您的解决方案的复杂性,您可能会发现一些其他的工件,因为VS.Net不能正确地重载项目,但是至少应该使用控制台生成。

Additionally, you might need to use similar conditional "hacks" to reference correct libraries.

此外,您可能需要使用类似的条件“hack”来引用正确的库。