ggtffg
ggtffg

Reputation: 323

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?

Upvotes: 4

Views: 1471

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236308

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

Upvotes: 3

Related Questions