首先,如果此前已有类似问题,敬请谅解。我已经进行了搜索,但是关于netstandard2.0的信息存在很多冲突和过时的情况。
我正在学习新的netstandard和核心迁移,并使用CLI dotnet命令构建了空的"hello world"项目。这里涉及到Nuget引用。
问题在于,从netstandard2.0类库到net461类库添加引用会导致错误(netstandard2.0 --> net461 reference)。
"cannot be added due to incompatible targeted frameworks between the two projects. Please review the project you are trying to add and verify that is compatible with the following targets"
这篇由Rich Lander撰写的微软文章声称我可以使用netstandard2.0来实现这一点。
同时,Immo Landwerth [MSFT] 在他的文章中似乎提到了通过兼容性 shim 实现可能性,我认为这可能仅通过 Nuget 引用实现:
https://blogs.msdn.microsoft.com/dotnet/2016/09/26/introducing-net-standard/
也许Scott Hanselman可以帮忙!
似乎netsandard2.0支持net461,".NET实现支持"网格显示。
https://learn.microsoft.com/en-us/dotnet/standard/net-standard
示例代码
已经创建了一个 GitHub 仓库来演示此内容
https://github.com/raxuser100/net_standard_references.git https://github.com/raxuser100/net_standard_references/tree/master
在上述的主分支上克隆并从命令提示符中运行以下内容:dotnet add .\classlib_netstandard2.0_ref_classLib_net461\classlib_netstandard2.0_ref_classLib_net461.csproj reference .\classlib_net461\classlib_net461.csproj
您将会收到错误提示。
csproj文件包含以下内容: classlib_net461.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net461</TargetFramework>
</PropertyGroup>
</Project>
classlib_netstandard2.0.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>
CLI DotNet 命令脚本
我使用以下 CLI 命令创建项目结构:
dotnet new console --name console_core2.0 --framework netcoreapp2.0
dotnet new classlib --name classlib_netstandard2.0 --framework netstandard2.0
dotnet new classlib --name classlib_net461 --target-framework-override net461
# netstandard2.0 --> net461 link
dotnet new classlib --name classlib_netstandard2.0_ref_classLib_net461 --framework netstandard2.0
<#
add project a reference
netstandard2.0 --> net461 link
this returns an error
cannot be added due to
incompatible targeted frameworks between the two projects. Please review the project you are trying to add and verify that is compatible with the following targets:
At line:1 char:1
Works if we edit classlib_net461.csproj and add multiple targets including netstandard2.0
<PropertyGroup>
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
</PropertyGroup>
#>
dotnet add .\classlib_netstandard2.0_ref_classLib_net461\classlib_netstandard2.0_ref_classLib_net461.csproj reference .\classlib_net461\classlib_net461.csproj
解决向classLib_net461添加多个目标的问题
如果我们编辑classlib_net461.csproj并添加包括netstandard2.0在内的多个目标,则可以解决此问题。
classlib_net461.csproj
<PropertyGroup>
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
</PropertyGroup>
现在当我们引用时,正如您下面所看到的那样,它能够正常工作:
classlib_netstandard2.0_ref_classLib_net461.csproj
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\classlib_net461\classlib_net461.csproj" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>
现在两个类库都针对netstandard2.0,这是否有效?
问题
有没有一个简单的图解来以简单易懂的方式阐明所有这些问题和注意事项!
为什么这样不起作用。如果一个空的netstandard2.0实现了大部分net461,那么它肯定具备支持裸的net461引用所需的一切。我知道有一些API不起作用,即WPF,但这些是裸项目,没有Nuget或直接二进制引用。
解决方法,这为什么有效?我们是否总是需要针对net461和netstandard2.0来定位classlib,以便它们可以被仅netstandard2.0 classlib引用。
使用较旧的.net框架类库(net4.3及以上版本)在netstandard2.0 classlib中的正确结构是什么。
这是如何工作的?