【问题标题】:.NET: Different target frameworks via project configuration.NET:通过项目配置不同的目标框架
【发布时间】:2018-07-22 13:18:50
【问题描述】:

我想通过 Visual Studio 2015 中的配置获得两个不同的 .net 框架目标。虽然对于参考,您可以编辑 CSPROJ 文件并添加条件,这似乎不适用于第一个中的 TargetFrameworkVersion PropertyGroup 的文件。我的印象是,该元素中的任何Condition 都会导致 VS 完全忽略该元素并回退到默认值“v4.0”。

有什么方法可以为不同的配置获得不同的目标框架版本?

这是我在 CSPROJ 文件中尝试的:

<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>

具有装配引用条件的类似方法可以正常工作。

编辑 我发现了一个类似的 Stackoverflow 问题: Targetting multiple .net framework versions by using different project configurations 并尝试了未接受的答案中建议的方法,以从第一个 PropertyGroup 块中删除 TargetFrameworkVersion,并编辑后面的 &lt;PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'OLD_Debug|AnyCPU' "&gt; 块以包含它,但我的程序集仍然为框架 3.5 编译无论我使用哪种配置。至少如果我使用 [System.Reflection.Assembly]::LoadFrom("C:\PATH\MyAssembly.dll").ImageRuntimeVersion 从 Powershell 查看程序集,我总是得到版本 2,而不是 4。

【问题讨论】:

  • 我之前通过让构建脚本将 TargetFrameworkVersion 作为参数传递给每个 MSBuild 来做到这一点。请注意,这可以在 Visual Studio 2017 中使用new .csproj 格式更轻松地完成:&lt;TargetFrameworks&gt;net35;net45;netstandard2.0&lt;/TargetFrameworks&gt;,它在 Visual Studio 中构建所有版本。

标签: c# .net visual-studio configuration


【解决方案1】:

this answer 中找到的类似问题的方法有效: 保留第一个 PropertyGroup 没有配置特定设置,从中删除 TargetFrameworkVersion 元素。并将TargetFrameworkVersion 设置添加到文件中的特定配置PropertyGroups,只需将它们加倍以进行调试/发布:

<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>

我验证如下:

  1. 我的程序集为 3.5 框架(OLD_... 配置)引用了 mscorlib 程序集的 2.0.0.0 版本,为 4.0 框架(NEW_... 配置)引用了 mscorlib 版本 4.0.0.0。
  2. 使用ILSpy,我发现我的3.5版本的程序集没有目标框架的属性,因为这是从版本4才引入的,但是版本4的框架作为程序集的属性出现:

    [assembly: TargetFramework(".NETFramework,Version=v4.0", FrameworkDisplayName = ".NET Framework 4")]
    

【讨论】:

    猜你喜欢
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    相关资源
    最近更新 更多