Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly.GetTypes() throwing an exception

Tags:

What does assembly GetTypes() do behind the scenes? Assuming an assembly has been loaded to the AppDomain does it still need to read from the physical DLL? And what the assembly manifest do?

Iterating through the assembly like this:

AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes())  

I'm occasionally getting the following error:

 Could not load file or assembly  

Which tells me that because an assembly is loaded into the AppDomain it's not necessarily fully loaded to memory. Sometimes it still needs to go back to the file.

My questions:

  1. Why is it doing that?
  2. What can I do to detect these semi-loaded assemblies?
like image 890
Alwyn Avatar asked Aug 11 '12 14:08

Alwyn


1 Answers

Getting a type from a assembly may require additional assemblies to be loaded so that is most probably the reason for the error; a failure to load a dependent assembly. However a .NET assembly may be constructed from several modules in different files so I believe you may also face this problem if you have a multifile assembly and one or more of the files are missing or corrupt.

Associated with the error you should get more information about the specific assembly that could not be loaded.

If you just want to load a list of the loadable types in the assembly you can use a extension method like this:

public static IEnumerable<Type> GetLoadableTypes(this Assembly assembly) {     if (assembly == null) throw new ArgumentNullException(nameof(assembly));     try     {         return assembly.GetTypes();     }     catch (ReflectionTypeLoadException e)     {         return e.Types.Where(t => t != null);     } } 

(Source: Get All Types in an Assembly)

like image 90
João Angelo Avatar answered Sep 21 '22 05:09

João Angelo