Why .NET Standard 2 build references many assemblies instead of single netstandard.dll assembly

大城市里の小女人 提交于 2021-02-17 05:15:13

问题


Unlike .NET Standard 1, .NET Standard 2 introduces a single netstandard.dll reference assembly.

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

The resulting assembly does contain a single reference to netstandard:

$ ikdasm test.exe | grep extern
.assembly extern netstandard

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.


回答1:


This is sadly due to history.

You are right that in theory only netstandard.dll is needed to build a .NET Standard library.

However, two things complicate this:

  1. .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 in netstandard.dll.
  2. 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 - from mscorlib.dll to the individual framework dlls.

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



来源:https://stackoverflow.com/questions/56148844/why-net-standard-2-build-references-many-assemblies-instead-of-single-netstanda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!