Selaa lähdekoodia

Resolve SRE support lazily instead of in cctor; closes #119

ghorsington 4 vuotta sitten
vanhempi
commit
acd0eb214f
1 muutettua tiedostoa jossa 16 lisäystä ja 7 poistoa
  1. 16 7
      BepInEx/Utility.cs

+ 16 - 7
BepInEx/Utility.cs

@@ -14,28 +14,39 @@ namespace BepInEx
 	/// </summary>
 	public static class Utility
 	{
+		private static bool? sreEnabled;
+
 		/// <summary>
 		/// Whether current Common Language Runtime supports dynamic method generation using <see cref="System.Reflection.Emit"/> namespace.
 		/// </summary>
-		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()
 		{