tags:

views:

317

answers:

3

I have a broken Assembly that I want to reflect over, its not broken badly, it just cannot find a referenced assembly, so it does fail a PEVerify. But....Assembly.LoadFrom() will still load it and GetTypes() will throw a ReflectionTypeLoadException, the .LoaderExceptions array shows me what referenced assembly cannot be found. At this point I am roadblocked.

However, the great little tool Reflector is able to go further and actually display the contained types, and handles gracefully the missing reference issue by giving me a pop-up dialog to browse for it. My question is, How after the GetTypes() fails does reflector manage to get the types anyway?

+4  A: 

Reflector doesn't use System.Reflection to analyse an assembly.

I don't know which library Reflector uses, but you might want to have a look at Cecil.

dtb
This does not answer the question at all. I am not trying to resolve the assembly, I am wanting to get all the type (names) despite the unresolved reference.
Tim Jarvis
Sorry, I misread your question. Answer updated.
dtb
Ah, I'll have a look at that. Thanks.
Tim Jarvis
A: 

Hi Tim

Not too sure if this will help, but perhaps try getting the the modules first and then the types from there.

Assembly a = Assembly.Load(brockenAssembly);

Module[] mList = a.GetModules();

for (int i = 0; i < mList.length; i++)
{
     Module m = a.GetModules()[i];
     Type[] tList = m.GetTypes();

}

Hope fully you could get the list of types available in one of the modules.

Yo Momma
A: 

Module.GetTypes() will throw this same exception. If you catch a ReflectionTypeLoadException from the Assembly.GetTypes() call, that exception will have a "Types" property on it of all the types that it could load. The ones that it couldn't will be detailed in a list/array property called LoaderExceptions.

Greg