Skip to content

Commit cd83a20

Browse files
authored
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.
1 parent e9ff32f commit cd83a20

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

Harmony/Tools/AccessTools.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,15 @@ public static Type[] GetTypesFromAssembly(Assembly assembly)
7373
{
7474
try
7575
{
76-
return assembly.GetTypes();
76+
// GetTypes can not throw on some assemblies with unloadable types and instead return an array with some nulls in it.
77+
// This is very rare so check first and only create a new array if something is actually found.
78+
var tarr = assembly.GetTypes();
79+
for (var i = 0; i < tarr.Length; i++)
80+
{
81+
if (tarr[i] == null)
82+
return tarr.Where(type => type is object).ToArray();
83+
}
84+
return tarr;
7785
}
7886
catch (ReflectionTypeLoadException ex)
7987
{

0 commit comments

Comments
 (0)