From cd83a206c5df0eaf7d9970ed3fbe4c8386739d01 Mon Sep 17 00:00:00 2001 From: ManlyMarco <39247311+ManlyMarco@users.noreply.github.com> Date: Wed, 20 Dec 2023 20:47:21 +0100 Subject: [PATCH] Fix AccessTools.GetTypesFromAssembly sometimes returning an array with a null in it (#80) GetTypes can sometimes not throw on some assemblies with unloadable types and instead return an array with some nulls in it. No idea why it happens exactly, but it is a thing. --- Harmony/Tools/AccessTools.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Harmony/Tools/AccessTools.cs b/Harmony/Tools/AccessTools.cs index c64d2fbd..2dc71813 100644 --- a/Harmony/Tools/AccessTools.cs +++ b/Harmony/Tools/AccessTools.cs @@ -73,7 +73,15 @@ public static Type[] GetTypesFromAssembly(Assembly assembly) { try { - return assembly.GetTypes(); + // GetTypes can not throw on some assemblies with unloadable types and instead return an array with some nulls in it. + // This is very rare so check first and only create a new array if something is actually found. + var tarr = assembly.GetTypes(); + for (var i = 0; i < tarr.Length; i++) + { + if (tarr[i] == null) + return tarr.Where(type => type is object).ToArray(); + } + return tarr; } catch (ReflectionTypeLoadException ex) {