Resolve SRE support lazily instead of in cctor

Rebase of acd0eb2
This commit is contained in:
Bepis committed 2020-11-06 00:13:30 +11:00
1 parent 8a6966d040
commit 78a9cf420b
1 file changed
+13 -5
+13 -5
View File
@@ -15,27 +15,35 @@ 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()
{
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;
}
/// <summary>