C# 如何确定运行.NET标准类库的.NET平台?
.NET标准库功能可能因运行它的.NET平台而异:C# 如何确定运行.NET标准类库的.NET平台?,c#,.net-core,.net-standard,.net-standard-2.0,C#,.net Core,.net Standard,.net Standard 2.0,.NET标准库功能可能因运行它的.NET平台而异: .NET框架 .NET核心 沙马林 如何检查.NET标准库当前运行的.NET平台 例如: // System.AppContext.TargetFrameworkName // returns ".NETFramework,Version=v4.6.1" for .NET Framework // and // returns null for .NET Core. if (IsNullOrWhiteSpace(System.AppC
- .NET框架
- .NET核心
- 沙马林
// 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框架和.NET核心而言)?很好。。其中一个问题是,除非您正在开发一个非常特定的跨平台库,否则一般来说,您不应该关心底层运行时实现是什么
但如果你真的必须这样做,那么推翻这一原则的一个方法是:
public enum Implementation { Classic, Core, Native, Xamarin }
public Implementation GetImplementation()
{
if (Type.GetType("Xamarin.Forms.Device") != null)
{
return Implementation.Xamarin;
}
else
{
var descr = RuntimeInformation.FrameworkDescription;
var platf = descr.Substring(0, descr.LastIndexOf(' '));
switch (platf)
{
case ".NET Framework":
return Implementation.Classic;
case ".NET Core":
return Implementation.Core;
case ".NET Native":
return Implementation.Native;
default:
throw new ArgumentException();
}
}
}
如果你想表现得更出色,那么你也可以引入不同的值(有更多细节)。。。其中一个问题是,除非您正在开发一个非常特定的跨平台库,否则一般来说,您不应该关心底层运行时实现是什么
但如果你真的必须这样做,那么推翻这一原则的一个方法是:
public enum Implementation { Classic, Core, Native, Xamarin }
public Implementation GetImplementation()
{
if (Type.GetType("Xamarin.Forms.Device") != null)
{
return Implementation.Xamarin;
}
else
{
var descr = RuntimeInformation.FrameworkDescription;
var platf = descr.Substring(0, descr.LastIndexOf(' '));
switch (platf)
{
case ".NET Framework":
return Implementation.Classic;
case ".NET Core":
return Implementation.Core;
case ".NET Native":
return Implementation.Native;
default:
throw new ArgumentException();
}
}
}
如果您想变得更加离谱,那么还可以为引入不同的值(提供更多详细信息)。使用System.Runtime.InteropServices
命名空间中的属性
返回一个字符串,该字符串指示运行应用程序的.NET安装的名称
该属性返回以下字符串之一:
- “.NET核心”
- “.NET框架”
- “.NET本机”
System.Runtime.InteropServices
命名空间中的属性
返回一个字符串,该字符串指示运行应用程序的.NET安装的名称
该属性返回以下字符串之一:
- “.NET核心”
- “.NET框架”
- “.NET本机”