问题
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:
- .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.
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