diff --git a/BepInEx/Utility.cs b/BepInEx/Utility.cs index 86990cc..9b48999 100644 --- a/BepInEx/Utility.cs +++ b/BepInEx/Utility.cs @@ -14,28 +14,39 @@ 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() { + if (sreEnabled.HasValue) + return sreEnabled.Value; + try { - CLRSupportsDynamicAssemblies = true; // 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; + } + + static Utility() + { CheckPlatform(); } @@ -255,8 +266,6 @@ namespace BepInEx } } - - // Adapted from https://github.com/MonoMod/MonoMod.Common/blob/master/Utils/PlatformHelper.cs#L13 private static void CheckPlatform() {