One fairly nasty way would be:
Type[] types;
try
{
types = asm.GetTypes();
}
catch (ReflectionTypeLoadException e)
{
types = e.Types;
}
foreach (var t in types.Where(t => t != null))
{
...
}
It’s definitely annoying to have to do this though. You could use an extension method to make it nicer in the “client” code:
public static IEnumerable<Type> GetLoadableTypes(this Assembly assembly)
{
// TODO: Argument validation
try
{
return assembly.GetTypes();
}
catch (ReflectionTypeLoadException e)
{
return e.Types.Where(t => t != null);
}
}
You may well wish to move the return
statement out of the catch block – I’m not terribly keen on it being there myself, but it probably is the shortest code…