c# - .NET:通过项目配置不同的目标框架

标签 c# .net visual-studio configuration

我想通过 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并尝试了未接受的答案中建议的方法来删除 TargetFrameworkVersion从第一个PropertyGroup block ,然后编辑后面的 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'OLD_Debug|AnyCPU' "> block 来包含它,但无论我使用哪种配置,我的程序集仍然是针对框架 3.5 进行编译的。至少如果我使用 [System.Reflection.Assembly]::LoadFrom("C:\PATH\MyAssembly.dll").ImageRuntimeVersion 从 Powershell 查看程序集,我总是得到版本 2,而不是版本 4。

最佳答案

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

<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 框架的 mscorlib 程序集版本 2.0.0.0(OLD_... 配置),以及 4.0 框架的 mscorlib 版本 4.0.0.0(NEW_. .. 配置)。
  2. 使用ILSpy ,我发现我的程序集的 3.5 版本没有目标框架的属性,因为这是从版本 4 开始才引入的,但版本 4 框架显示为程序集的属性:

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

关于c# - .NET:通过项目配置不同的目标框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48745318/

相关文章:

c# - 当值类型可为空时,为什么 ApiController 返回空字典?

C# - 在动态生成的程序集中引用类型

.net - MVVM 应用程序中的并发架构

c# - 是否可以选择一列的总和,按另一列分组?

c# - 如何在 C# 应用程序上连接到 sql server

c# - 是否可以为 XAML 绑定(bind)到 View 模型启用智能感知?

c# - 为什么签名程序集加载缓慢?

visual-studio - Visual Studio XAML 编辑器中令人难以忍受的自动换行 - 2010 年是否有任何缓解?

visual-studio - Resharper 的替代品

git - 从 Visual Studio 推送 Git 存储库时运行 ssh.exe 时出错