如何确定运行.NET Standard 类库的.NET 平台? [英] How to determine the .NET platform that runs the .NET Standard class library?

查看:33
本文介绍了如何确定运行.NET Standard 类库的.NET 平台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET 标准库 — 一个库统治他们.

.NET 标准库功能可能因运行它的 .NET 平台而异:

The .NET Standard Library functionality may vary depending on the .NET platform that runs it:

  • .NET 框架
  • .NET 核心
  • Xamarin

如何查看 .NET Standard 库当前运行所在的 .NET 平台?

How to check the .NET platform where the .NET Standard library currently runs?

例如:

// 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

这是回答问题的可靠方法吗(至少对于 .NET Framework 和 .NET Core)?

Is it reliable way to answer the question (at least for .NET Framework and .NET Core)?

推荐答案

使用 RuntimeInformation.FrameworkDescription 来自 System.Runtime.InteropServices 命名空间的属性.

Use the RuntimeInformation.FrameworkDescription Property from the System.Runtime.InteropServices namespace.

返回一个字符串,该字符串指示运行应用的 .NET 安装的名称.

Returns a string that indicates the name of the .NET installation on which an app is running.

该属性返回以下字符串之一:

The property returns one of the following strings:

  • .NET Core".

  • ".NET Core".

.NET 框架".

.NET 原生".

这篇关于如何确定运行.NET Standard 类库的.NET 平台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆