ReflectionTypeLoadException From GetTypes()

  • Thread starter Thread starter Antony
  • Start date Start date
A

Antony

Hi all,

I could really use your help here. I am trying to create an instance of a
class defined in one of my assemblies, from a method in the same assembly.
The problem area of code looks something like this...

Assembly runningAssembly = Assembly.GetExecutingAssembly();
Type[] assemblyTypes = runningAssembly.GetTypes();

// Now I have some types, find the one I'm looking for and use
Activator.CreateInstance
// to return an instance...

Seems pretty straightforward really, doesn't it?

Well, the GetTypes() call fails with a ReflectionTypeLoadException "One or
more of the types in the assembly unable to load". I've done a fair bit of
searching and on several occassions have seen the 'make sure all dependent
assemblies are in the same directory or GAC', which is fair enough. As far
as I am aware, all of this assemblies dependencies *are* in the same
directory or the GAC. Having checked ILDASM manifest information for
externs, it looks okay to me. I've also checked the Fusion log and that
seems to be as expected also.

The top of the stack trace looks something like this...

at System.Reflection.Module.GetTypesInternal(StackCrawlMark& stackMark)
at System.Reflection.Assembly.GetTypes()
at ... [the calling line in my code] ...

Are there any other places I should be looking for dependencies? I have
noticed that some of the manifest version numbers do not match the actual
version (manifest version always older) but I don't really know what to do
about that, even if it is relevant. Are there any other cases in which this
exception would be thrown? I'm stumped right now.

If anybody can shed some light on this I would be really grateful.

Thanks in advance.

Antony.
 
Right. I have figured it out.

I had a bug in my class hierarchy that caused a decendent class to reduce
the accessibility of an overriden function from the ancestor class. It
wasn't picked up at compile time but I found the information in the
ReflectionTypeLoadException object.

The ReflectionTypeLoadException object has a "Types" property and a
"LoaderExceptions" property. The LoaderExceptions property is an array of
exceptions thrown when trying to load the types. In my LoaderExceptions
there were two entries, one for each of the descendent functions I had got
wrong.

Cheers,

Antony.
 
Back
Top