ReflectionTypeLoadException


If a type can’t be loaded for some reason during a call to Module.GetTypes(), ReflectionTypeLoadException will be thrown. Assembly.GetTypes() also throws this because it calls Module.GetTypes().


The Message for this exception is “One or more exceptions have been thrown while loading the types” or “Unable to load one or more of the types in the assembly”, which doesn’t seem very descriptive. But, the exception actually provides more info that that. Just get the LoaderExceptions property of the ReflectionTypeLoadException instance. That will give an array of the exceptions caught while loading all of the types from the module. If the exceptions are due to an assembly loading problem, see my general debugging advice.

Comments (11)

  1. If you get this exception, the first thing to check is whether you made a typo (or rather an intellisenso) and used Assembly.LoadFile instead of Assembly.LoadFrom.

  2. Flenngard & Whris says:

    Thanks sooo much for the advice about using LoadFrom istead!!! of LoadFile! Worked awesome

  3. Great advice suzcook. After looking at the LoaderExceptions I found that I was missing a couple References in my project. The solution built because the references were not needed until run time.

    Thanks Again

  4. Pankil says:

    Hi Suzanne,

    I have a problem with dynamically loading an assembly which is refering to another assembly. That assembly is loaded in GAC but unfortunately there is a reference missing in the assembly loaded in GAC.

    When I try to use oAssembly.GetTypes(). Error I am getting is ReflectionTypeLoadException Error. I have checked the LoaderExceptions error there is FileNotFound exception.

    What I want is to load all types and its method and properties. In the same way as it loads in the VS.Net Object Browser. Is this possible?

    Coz, the same assembly’s Type, Methods, Properties and fields are displayed by the Object Browser.

    Regards,

    Pankil

  5. ulu says:

    I’m trying to fix my ASP.NET application, it’s working OK at my development machine, but when I upload it it crashes. I’m unable to use Fusion logs since it’s at the host’s machine.

    The exception is thrown at Module.GetTypes().

    I’ve tried to use the LoaderExceptions property, and all I get is several identical messages "System.TypeLoadException: ?" (actually my assembly contains much more types).

    I’m using .NET 1.1

    Could anybody help me with that? Could it be some permissions issue?

    Thanks a lot

    ulu

  6. ulu says:

    Well it turned out that I had an old version of a 3rd party dll.

    This is how it can be found out:

    1. Enumerate all your ex.LoaderExceptions

    2. If they are TypeLoadExceptions, look at TypeLoadException.TypeName

    ulu

  7. thank you for the tip with the loaderexceptions. After I extended my logger to watch for those i was able to find "my" culbrit.

  8. Rajesh says:

    Damn good!!! I was in trouble because of some missing references.!!! Helped me a lot :))

  9. It’s natural for a tool to use Reflection-Only loading to load an assembly and view the types in it.

  10. Peter says:

    I wonder how Reflector can obtain the types of a loaded assembly. Reflector lists the references and if clicked on a type that cannot be found, pops up a dialog box in order to tell Reflector the path. But Reflector can obtain all the classes of the loaded assembly that are NOT referenced. What’s the trick? Is there something else than GetTypes()?

  11. Lailah says:

    Looking at LoaderExceptions property is a great tip. I also found that some references were missing.