简体   繁体   English

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

[英]Change the application target framework when changing build configurations in Visual Studio

I have these build configurations: 我有这些构建配置:

在此输入图像描述

These platform configurations: 这些平台配置:

在此输入图像描述

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. 我的解决方案是一个庞大的API,包含20个解决方案,其中一些解决方案使用Async关键字和其他只能从.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#高级答案无法帮助我。

My suggestion is to get rid of conditional statements in code by moving platform/target/etc sencitive code in partial files. 我的建议是通过在部分文件中移动platform / target / etc sencitive代码来摆脱代码中的条件语句。 Then I would go to project file and would make the icluded files sensitive on particular condition using all the fuctionality ms-build provides 然后我会转到项目文件,并使用所有功能ms-build提供的特定条件使包含的文件敏感

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 右键单击项目文件,然后按“ 编辑项目”
  • 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 右键单击项目文件,然后按“ 重新加载”

now when you run the project undo debug you will get: 现在,当您运行项目撤消调试时,您将获得:

Hello 4.0 Word! 你好4.0 Word!

undo release you willl get: 撤消发布你会得到:

Hello 4.5 Word! 你好4.5字!

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. 请注意Condition语句 - 它描述了为Debug, AnyCPU配置指定的此特定属性集。

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; 请注意,VS.Net GUI不支持此功能,并且不会在项目“属性”窗口中显示正确的值; though it will use these values for build. 虽然它会使用这些值进行构建。

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. 此外,您可能需要使用类似的条件“黑客”来引用正确的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM