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

This commit is contained in:
ghorsington committed 2020-06-20 22:50:17 +03:00
1 parent 82c4e19ad8
commit acd0eb214f
1 file changed
+16 -7
+16 -7
View File
@@ -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()
{