3

Possible Duplicate:
How to prevent ReflectionTypeLoadException when calling Assembly.GetTypes()

I would like to get all the types in an assembly. However, I get the following error:

System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.

The problem is the assembly I am getting the types from is referencing another assembly that is only available in the production environment, and not within the unit test environment.

So, is there any way that I can filter GetTypes or something similar to only return the types actually defined in the assembly and not get the type load exception?

e.g. replacement for

.Assembly.GetTypes().Where(t => t.Namespace.Equals(...
Community
  • 1
  • 1
sweetfa
  • 5,457
  • 2
  • 48
  • 62
  • might or might not be applicable, but i would recommend that you use GetExportedTypes instead, unless you need to see the private types inside the assembly. http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getexportedtypes.aspx – Phil Oct 14 '12 at 20:31
  • GetExportedTypes has exactly the same issue. Certainly could use either method, however it does not overcome the underlying issue of accessing references in different assemblies. – sweetfa Oct 14 '12 at 20:33
  • Why doesn't everybody link to the [Get All Types in an Assembly](http://haacked.com/archive/2012/07/23/get-all-types-in-an-assembly.aspx) blog post we all know about?! Also, see [Jon Skeet's answer](http://stackoverflow.com/questions/7889228/how-to-prevent-reflectiontypeloadexception-when-calling-assembly-gettypes) which is bound to get copied a lot here – sehe Oct 14 '12 at 21:58

1 Answers1

9

GetTypes only gets the types that are defined in the assembly, however, you may not be able to load them because they are referencing types that are in an assembly you have not loaded or cannot be found. For example, if you try to load a type that derives from a class in this other assembly then you get a ReflectionTypeLoadException. You can get the types that you did load from the exception object's Types property. Note that there will be a null for each type you could not load and the LoaderExceptions property has an exception for each of them.

public static Type[] GetTypesLoaded(Assembly assembly)
{
    Type[] types;
    try
    {
      types = assembly.GetTypes();
    }
    catch (ReflectionTypeLoadException e)
    {
      types = e.Types.Where(t => t != null).ToArray();
    }

    return types;    
}
Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
  • -1 The question clearly mentioned "not get the type load exception" and you again did it the try catch way. – Rikki Oct 14 '12 at 20:16
  • 4
    @MohammadGoudarzi There is no way to "not get the type load exception" you can only catch it get the types that you where able to load. – Mike Zboray Oct 14 '12 at 20:19
  • What about "Assembly.GetExportedTypes()"? – Rikki Oct 15 '12 at 05:43
  • 2
    @MohammadGoudarzi It does not work either. In fact it gives you no ability to recover and continue since it throws a `FileNotFoundException` and you have no idea what types could or could not be loaded. – Mike Zboray Oct 15 '12 at 06:20
  • Could you please share the assembly you are trying to load for me? – Rikki Oct 15 '12 at 16:15
  • Like the description states, currently this is the only way to return types for an assembly that bypasses issues with referencing types that aren't loaded. – Phil Cooper Jan 07 '15 at 18:24