[英]Why .NET framework project can be added as reference in .NET Standard 2.0 project?
While doing some conversion of .NET Framework 4.6.2 project into .NET Standard project, I noticed that I could add .NET Framework project reference in the .NET Standard project, which doesn't sound correct theoretically. While doing some conversion of .NET Framework 4.6.2 project into .NET Standard project, I noticed that I could add .NET Framework project reference in the .NET Standard project, which doesn't sound correct theoretically.
Why .NET framework project can be added as reference in .NET Standard 2.0 project?为什么 .NET 框架项目可以作为参考添加到 .NET 标准 2.0 项目中?
When you have .NET Standard project it is compiled against netstandard.dll.当您拥有 .NET 标准项目时,它是根据 netstandard.dll 编译的。 Both .NET Framework and .NET Core have netstandard.dll (which provides type-forwarding to mscorlib.dll or System.Runtime.dll).
.NET Framework 和 .NET Core 都具有 netstandard.dll(它提供类型转发到 mscorlib.Z06416233FE5EC4CAF133122)。 That is why you can use .NET Standard project both in .NET Core and .NET Framework.
这就是为什么您可以在 .NET 核心和 .NET 框架中使用 .NET 标准项目的原因。
So what happens when your .NET Standard project references .NET Framework project (which is compiled against mscorlib.dll)?那么当您的 .NET 标准项目引用 .NET 框架项目(针对 mscorlib.dll 编译)时会发生什么?
If you have references chain .NET Framework -> .NET Standard -> .NET Framework then there is no problem at all because at runtime you have mscorlib.dll with all required types.如果您有参考链.NET 框架 -> .NET 标准 -> .NET 框架,那么根本没有问题,因为在运行时您有 mscorlib.Z064316233E.4AB2485 所需的所有类型。
But what about .NET Core -> .NET Standard -> .NET Framework references?但是.NET 核心 -> .NET 标准 -> .NET 框架参考呢? At runtime you have only System.Runtime.dll.
在运行时,您只有 System.Runtime.dll。 There is no problem with .NET Standard project because netstandard.dll will type-forward to System.Runtime.dll.
.NET 标准项目没有问题,因为 netstandard.dll 将类型转发到 System.Runtime.dll。 But the trick is that .NET Core has also mscorlib.dll , And yes.
但诀窍是.NET 核心也有 mscorlib.dll ,是的。 it's also only type-forwarding to System.Runtime.dll types.
它也只是类型转发到 System.Runtime.dll 类型。 And everything works.
一切正常。 ...unless you will use some .NET Framework type which does not exist in System.Runtime.dll.
...除非您将使用 System.Runtime.dll 中不存在的某些 .NET 框架类型。
Further reading: .NET Framework Compatibility Shim进一步阅读:.NET 框架兼容性垫片
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.