简体   繁体   中英

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.

Why .NET framework project can be added as reference in .NET Standard 2.0 project?

When you have .NET Standard project it is compiled against netstandard.dll. Both .NET Framework and .NET Core have netstandard.dll (which provides type-forwarding to mscorlib.dll or System.Runtime.dll). That is why you can use .NET Standard project both in .NET Core and .NET Framework.

So what happens when your .NET Standard project references .NET Framework project (which is compiled against 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.

But what about .NET Core -> .NET Standard -> .NET Framework references? At runtime you have only System.Runtime.dll. There is no problem with .NET Standard project because netstandard.dll will type-forward to System.Runtime.dll. But the trick is that .NET Core has also mscorlib.dll , And yes. it's also only type-forwarding to System.Runtime.dll types. And everything works. ...unless you will use some .NET Framework type which does not exist in System.Runtime.dll.

Further reading: .NET Framework Compatibility Shim

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM