gpt4 book ai didi

c# - 如何检测我的库是否在 .Net Core 3 桌面包下运行

转载 作者:行者123 更新时间:2023-11-30 21:31:10 24 4
gpt4 key购买 nike

这是我现在可以检测到它的方法(此方法基于桌面包类中的内部更改):

public static class FrameworkVersions {
static readonly bool f_nativeMatrix_Exists;
static FrameworkVersions() {
f_nativeMatrix_Exists= typeof(System.Drawing.Drawing2DMatrix)
.GetField("nativeMatrix", BindingFlags.Instance | BindingFlags.NonPublic) != null;
}
public static bool IsNetCore3DesktopPackage {
get{ return !f_nativeMatrix_Exists; }
}
}

是否存在最好的方法?请分享您的经验。

最佳答案

您可以信赖RuntimeInformation.FrameworkDescription就像 .NET Core 中使用的一样 tests :

//using System.Runtime.InteropServices;
bool IsFullFramework = RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework",
StringComparison.OrdinalIgnoreCase);
bool IsNetNative = RuntimeInformation.FrameworkDescription.StartsWith(".NET Native",
StringComparison.OrdinalIgnoreCase);
bool IsNetCore = RuntimeInformation.FrameworkDescription.StartsWith(".NET Core",
StringComparison.OrdinalIgnoreCase);

您还可以通过查找 TargetFrameworkAttribute 来检测正在运行的框架版本装配体(如 Rick Strahl 的 blog post):

//using System.Reflection;
//using System.Runtime.Versioning;
var framework = Assembly.GetEntryAssembly()?
.GetCustomAttribute<TargetFrameworkAttribute>()?
.FrameworkName;
MessageBox.Show(framework);

关于c# - 如何检测我的库是否在 .Net Core 3 桌面包下运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53664274/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com