My application generates diagrams from the code structure of .NET applications. Assembly.LoadFile
works, but only if my application has been put in the same folder as the assembly that I'm loading (seems to be due to referenced assemblies); if its not in the same folder, I get a ReflectionTypeLoadException
exception if the loaded assembly had dependencies not in the GAC.
I've tried using Assembly.ReflectionOnlyLoadFrom
instead.
AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += ResolveAssembly;
Assembly loadedAssembly = Assembly.ReflectionOnlyLoadFrom(FileName);
With the following event:
Assembly ResolveAssembly(object sender, ResolveEventArgs args){
return Assembly.ReflectionOnlyLoad(args.Name);
}
But that ResolveAssembly
only got called for System
, System.Windows.Forms
, and System.Drawing
. It did not get called for OpenTK.dll
, OpenTK.GLControl.dll
and other referenced assemblies. And it too threw a ReflectionTypeLoadException
when I called loadedAssembly.GetTypes()
.
How can I get it to load the referenced assemblies, so that I don't have to enforce my application be put in the same folder as the assembly its inspecting?
Update:
When I call Assembly.ReflectionOnlyLoadFrom
, I can use GetReferencedAssemblies()
to observe the references from that assembly. When I examine AppDomain.CurrentDomain.ReflectionOnlyGetAssemblies()
, I notice only a single assembly was loaded (the one originally specified). In otherwords, the referenced assemblies are simply not being loaded (despite the ReflectionOnlyAssemblyResolve
hook).