如果使用条件目标框架,Nuget 会为错误的目标框架恢复包

问题描述

我有一个解决方案,它通过选择相应的配置来构建 .NetFramework 3.5 或 .NetFramework 4.5.2 库。我正在尝试将其迁移到 .NET5,但我也想保留构建 .NetFramework 库的可能性。由于解决方案中包含一些不支持多目标的 CLI/C++ 项目,我决定使用条件 TargetFrameworks:

<PropertyGroup Condition=" '$(Configuration)' == 'Debug_Net5' Or '$(Configuration)' == 'Release_Net5' ">
    <TargetFramework>net5.0-windows</TargetFramework>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)' == 'Debug_Net4' Or '$(Configuration)' == 'Release_Net4' ">
    <TargetFramework>net452</TargetFramework>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)' == 'Debug' Or '$(Configuration)' == 'Release' ">
    <TargetFramework>net35</TargetFramework>

解决方案已通过 Visual Studio 成功构建。但是,如果我尝试使用 MSBuild 进行构建,请通过 Nuget restore 调用

"nuget.exe" restore <mySolutionPath> -Verbosity detailed
msbuild <mySolutionPath> /p:Configuration=Debug_Net4;Platform=x86

我收到错误

C:\Program Files\dotnet\sdk\5.0.302\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(241,5): error NETSDK1005: Assets file '<projectpath>\obj\project.assets.json' doesn'
t have a target for 'net452'. Ensure that restore has run and that you have included 'net452' in the TargetFrameworks for your project.

根据 nuget restore 输出和资产文件内容,仅对 .NetFramework 3.5 进行了恢复。有没有办法为 nuget restore 指定 targetFramework?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)