Skip to content

Commit

Permalink
Fix AccessTools.GetTypesFromAssembly sometimes returning an array wit…
Browse files Browse the repository at this point in the history
…h 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.
  • Loading branch information
ManlyMarco authored Dec 20, 2023
1 parent e9ff32f commit cd83a20
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Harmony/Tools/AccessTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit cd83a20

Please sign in to comment.