From 78a9cf420b94c6411a45cba4628bbe712ef11872 Mon Sep 17 00:00:00 2001 From: Bepis <36346617+bbepis@users.noreply.github.com> Date: Fri, 6 Nov 2020 00:13:30 +1100 Subject: [PATCH] Resolve SRE support lazily instead of in cctor Rebase of acd0eb2 --- BepInEx.Core/Utility.cs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/BepInEx.Core/Utility.cs b/BepInEx.Core/Utility.cs index 0fcb356..4da2fd7 100644 --- a/BepInEx.Core/Utility.cs +++ b/BepInEx.Core/Utility.cs @@ -15,27 +15,35 @@ namespace BepInEx /// public static class Utility { + private static bool? sreEnabled; + /// /// Whether current Common Language Runtime supports dynamic method generation using namespace. /// - public static bool CLRSupportsDynamicAssemblies { get; } + public static bool CLRSupportsDynamicAssemblies => CheckSRE(); - static Utility() + private static bool CheckSRE() { try { - CLRSupportsDynamicAssemblies = true; + if (sreEnabled.HasValue) + return sreEnabled.Value; + // ReSharper disable once AssignNullToNotNullAttribute - var m = new CustomAttributeBuilder(null, new object[0]); + _ = new CustomAttributeBuilder(null, new object[0]); } catch (PlatformNotSupportedException) { - CLRSupportsDynamicAssemblies = false; + sreEnabled = false; + return sreEnabled.Value; } catch (ArgumentNullException) { // Suppress ArgumentNullException } + + sreEnabled = true; + return sreEnabled.Value; } ///