DmitryG
DmitryG

Reputation: 17850

How to detect that my library is running under .Net Core 3 Desktop Package

Here is how I can detect it now (this approach is based on internal changes in Desktop Package classes):

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; }
    }
}

Is the best way exists? Please share your experience.

Upvotes: 3

Views: 443

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125197

You can rely on RuntimeInformation.FrameworkDescription like what is used in .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);

You can also detect the running framework version by finding TargetFrameworkAttribute of the assembly (like this blog post by Rick Strahl):

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

Upvotes: 5

Related Questions