Tim Jarvis
Tim Jarvis

Reputation: 18815

How does reflector show types when Assembly.GetTypes() fails due to a missing referenced assembly

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?

Upvotes: 3

Views: 1378

Answers (3)

Greg Biles
Greg Biles

Reputation: 941

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.

Upvotes: 4

Yo Momma
Yo Momma

Reputation: 8781

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.

Upvotes: 0

dtb
dtb

Reputation: 217313

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.

Upvotes: 5

Related Questions