.NET Standard Library — One library to rule them all.
The .NET Standard Library functionality may vary depending on the .NET platform that runs it:
- .NET Framework
- .NET Core
- Xamarin
How to check the .NET platform where the .NET Standard library currently runs?
For example:
// System.AppContext.TargetFrameworkName
// returns ".NETFramework,Version=v4.6.1" for .NET Framework
// and
// returns null for .NET Core.
if (IsNullOrWhiteSpace(System.AppContext.TargetFrameworkName))
// the platform is .NET Core or at least not .NET Framework
else
// the platform is .NET Framework
Is it reliable way to answer the question (at least for .NET Framework and .NET Core)?
Well.. one of the main ideas behind .NET Standard is that unless you're developing a very specific cross-platform library then in general you should not be caring what the underlying runtime implementation is.
But if you really must then one way of overriding that principle is this:
And if you want to be even more egregious, then you can bring in the different values for Xamarin.Forms.Device.RuntimePlatform also (with more details here).
Use the
RuntimeInformation.FrameworkDescription
Property from theSystem.Runtime.InteropServices
namespace.