From 6adbe4d0274f83be4a8d8ed478a7f39f9fb64260 Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Mon, 2 Sep 2024 23:43:30 +0200 Subject: [PATCH 1/3] Remove reflection from TargetFrameworkName retrieval (forwarded to AppDomain on NetFX) --- .../src/System/AppContextDefaultValues.cs | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/Common/src/System/AppContextDefaultValues.cs b/src/Microsoft.DotNet.Wpf/src/Common/src/System/AppContextDefaultValues.cs index 81a5baec8e3..6fb7700e59d 100644 --- a/src/Microsoft.DotNet.Wpf/src/Common/src/System/AppContextDefaultValues.cs +++ b/src/Microsoft.DotNet.Wpf/src/Common/src/System/AppContextDefaultValues.cs @@ -74,22 +74,7 @@ private static void ParseTargetFrameworkName(out string identifier, out string p /// TargetFrameworkMoniker on .NET Framework and .NET Core 3.0+; null on .NET Core 2.2 or older runtimes private static string GetTargetFrameworkMoniker() { - try - { - var pSetupInformation = typeof(AppDomain).GetProperty("SetupInformation"); - object appDomainSetup = pSetupInformation?.GetValue(AppDomain.CurrentDomain); - Type tAppDomainSetup = Type.GetType("System.AppDomainSetup"); - var pTargetFrameworkName = tAppDomainSetup?.GetProperty("TargetFrameworkName"); - - return - appDomainSetup != null ? - pTargetFrameworkName?.GetValue(appDomainSetup) as string : - null; - } - catch (Exception) - { - return null; - } + return AppContext.TargetFrameworkName; } // This code was a constructor copied from the FrameworkName class, which is located in System.dll. From 1747bdb25cf584b78ad15a5e9c599a1a58d2363e Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Tue, 3 Sep 2024 00:18:33 +0200 Subject: [PATCH 2/3] Save additional ms by not parsing TargetFrameworkName on .NET Core --- .../Common/src/System/AppContextDefaultValues.cs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/Common/src/System/AppContextDefaultValues.cs b/src/Microsoft.DotNet.Wpf/src/Common/src/System/AppContextDefaultValues.cs index 6fb7700e59d..b1331cd912f 100644 --- a/src/Microsoft.DotNet.Wpf/src/Common/src/System/AppContextDefaultValues.cs +++ b/src/Microsoft.DotNet.Wpf/src/Common/src/System/AppContextDefaultValues.cs @@ -19,15 +19,19 @@ internal static partial class AppContextDefaultValues { public static void PopulateDefaultValues() { - string platformIdentifier, profile; - int version; - - ParseTargetFrameworkName(out platformIdentifier, out profile, out version); +#if NETFX + // Get Target Framework information + ParseTargetFrameworkName(out string platformIdentifier, out string profile, out int version); // Call into each library to populate their default switches PopulateDefaultValuesPartial(platformIdentifier, profile, version); +#else + // Call into each library to populate their default switches + // ".NETCoreApp,Version=v3.0" + PopulateDefaultValuesPartial(".NETCoreApp", string.Empty, 30000); +#endif } - +#if NETFX /// /// We have this separate method for getting the parsed elements out of the TargetFrameworkName so we can /// more easily support this on other platforms. @@ -181,7 +185,7 @@ private static bool TryParseFrameworkName(String frameworkName, out String ident return true; } - +#endif // This is a partial method. Platforms (such as Desktop) can provide an implementation of it that will read override value // from whatever mechanism is available on that platform. If no implementation is provided, the compiler is going to remove the calls // to it from the code From 9a4f8317d5c0edc439595a39053edd5fc22d806a Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Wed, 4 Sep 2024 19:04:18 +0200 Subject: [PATCH 3/3] Remove remarks that we've rendered obsolete --- .../src/Common/src/System/AppContextDefaultValues.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/Common/src/System/AppContextDefaultValues.cs b/src/Microsoft.DotNet.Wpf/src/Common/src/System/AppContextDefaultValues.cs index b1331cd912f..8ed032a1242 100644 --- a/src/Microsoft.DotNet.Wpf/src/Common/src/System/AppContextDefaultValues.cs +++ b/src/Microsoft.DotNet.Wpf/src/Common/src/System/AppContextDefaultValues.cs @@ -71,10 +71,6 @@ private static void ParseTargetFrameworkName(out string identifier, out string p /// /// This is equivalent to calling AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName /// - /// - /// AppDomain.CurrentDomain.SetupInformation is not available until .NET Core 3.0, but we - /// have a need to run this code in .NET Core 2.2, we attempt to obtain this information via Reflection. - /// /// TargetFrameworkMoniker on .NET Framework and .NET Core 3.0+; null on .NET Core 2.2 or older runtimes private static string GetTargetFrameworkMoniker() {