Skip to content

Commit

Permalink
Work around type load errors in .NET Framework
Browse files Browse the repository at this point in the history
  • Loading branch information
khellang committed Sep 26, 2024
1 parent d7f676e commit face8c7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/Scrutor/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,23 @@ private static bool GenericParametersMatch(IReadOnlyList<Type> parameters, IRead
return true;
}

// Workaround for type load issues in .NET Framework
public static IReadOnlyCollection<Type> GetLoadableTypes(this Assembly assembly)
{
try
{
return assembly.GetTypes();
}
catch (ReflectionTypeLoadException ex)
{
return ex.Types.Where(t => t is not null).ToArray()!;
}
catch
{
return [];
}
}

public static string ToFriendlyName(this Type type)
{
return TypeNameHelper.GetTypeDisplayName(type, includeGenericParameterNames: true);
Expand Down
4 changes: 2 additions & 2 deletions src/Scrutor/TypeSourceSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public IImplementationTypeSelector FromCallingAssembly()
{
return FromAssemblies(Assembly.GetCallingAssembly());
}

/// <summary>
/// Always uses the Scrutor assembly.
/// </summary>
Expand Down Expand Up @@ -188,7 +188,7 @@ private IImplementationTypeSelector InternalFromAssembliesOf(IEnumerable<Type> t

private IImplementationTypeSelector InternalFromAssemblies(IEnumerable<Assembly> assemblies)
{
return AddSelector(assemblies.SelectMany(asm => asm.GetTypes()));
return AddSelector(assemblies.SelectMany(asm => asm.GetLoadableTypes()));
}

private static ISet<Assembly> LoadAssemblies(ISet<AssemblyName> assemblyNames)
Expand Down

0 comments on commit face8c7

Please sign in to comment.