问题描述
与 .NET Standard 1 不同,.NET Standard 2 引入了单个 netstandard.dll
参考程序集.
Unlike .NET Standard 1, .NET Standard 2 introduces a single netstandard.dll
reference assembly.
但是,当使用 dotnet build
编译(使用 .NET Core SDK 2.2.203)时,我们可以看到它添加了对许多程序集(113)的引用,都在 netstandard.library/2.0.3/build/netstandard2.0/ref/*.dll
.这些引用之一是 netstandard.dll
.
However, when compiling (with .NET Core SDK 2.2.203) with dotnet build
we can see that it adds references to many assemblies (113), all under netstandard.library/2.0.3/build/netstandard2.0/ref/*.dll
. One of these references is netstandard.dll
.
$ grep TargetFramework *.csproj
<TargetFramework>netstandard2.0</TargetFramework>
$ dotnet build --verbosity normal | grep -o netstandard.library/2.0.3/build/netstandard2.0/ref | wc -l
113
生成的程序集确实包含对 netstandard 的单个引用:
The resulting assembly does contain a single reference to netstandard:
$ ikdasm test.exe | grep extern
.assembly extern netstandard
问题是为什么 netstandard.library
NuGet 包包含所有这些其他程序集,以及为什么在构建期间使用它们,而不是引用单个 netstandard.dll
.
The question is why netstandard.library
NuGet package contains all these other assemblies, and why are they used during build, instead of referencing a single netstandard.dll
.
推荐答案
这是历史造成的遗憾.
您说得对,理论上只需要 netstandard.dll
即可构建 .NET Standard 库.
You are right that in theory only netstandard.dll
is needed to build a .NET Standard library.
然而,有两件事使这变得复杂:
However, two things complicate this:
- .NET Standard 1.0-1.6 库使用了更多的 NuGet 包和程序集.因此,.NET Standard 1.6 库(.dll 文件)可能包含对例如的引用
System.Collections.Generic.dll
可解析.此外,构建系统需要知道 1.6 库期望在此 dll 中的类实际上与netstandard.dll
中的类相同. - 允许从 .NET Standard(和 .NET Core)库使用现有的 .NET Framework 库,而无需重新编译.这意味着它们也是为 .NET Framework 使用的
.dll
文件构建的 - 从mscorlib.dll
到单个框架 dll.
- .NET Standard 1.0-1.6 libraries used a lot more NuGet packages and thus assemblies. So a .NET Standard 1.6 library (.dll file) may contain references to e.g.
System.Collections.Generic.dll
to be resolvable. Also, the build system needs to know that classes that the 1.6 library expects in this dll is actually the same as the one innetstandard.dll
. - Existing .NET Framework libraries are allowed to be consumed from .NET Standard (and .NET Core) libraries without needing to be recompiled. This means that they also have been built for the
.dll
files that .NET Framework uses - frommscorlib.dll
to the individual framework dlls.
对于这两种情况,添加了对具有这些名称(和版本)的程序集的引用,这些程序集仅包含类型前向声明.因此,例如 mscorlib.dll
将包含一个空的 System.Object
条目,该条目标记为在 netstandard.dll
中查找".
For both of these, references to assemblies with these names (and versions) are added which contain only type forward declarations. So for example mscorlib.dll
would contain an empty System.Object
entry that is marked with "go look for that in netstandard.dll
".
这篇关于为什么 .NET Standard 2 构建引用许多程序集而不是单个 netstandard.dll 程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!