Here's the scenario using Assembly.ReflectionOnlyLoadFrom:
Both my assembly Inspected and my reflection Application Inspector reference Assembly Dependency.
If Inspector references Dependency 1.0.0.0 and Inspected references Dependency 1.1.0.0, Inspector cannot reflect over any types or methods in Inspected that use a type from Dependency. The moment such a type is hit i get:
System.IO.FileLoadException: Could not load file or assembly 'Dependency, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
However, Inspector can reflect over Dependency 1.1.0.0 itself just fine, so loading Dependency 1.1.0.0 as Assembly.ReflectionOnlyLoadFrom does work from an assembly that's already using Dependency 1.0.0.0.
Here is the code i use to load an assembly and preload it's dependencies:
var assembly = Assembly.ReflectionOnlyLoadFrom(assemblyPath);
foreach (var assemblyName in assembly.GetReferencedAssemblies()) {
Assembly.ReflectionOnlyLoad(assemblyName.FullName);
}
It's not an issue with Dependency 1.1.0.0 not being resolved, as i've set a breakpoint in the foreach and confirmed it is loaded and also checked AppDomain.CurrentDomain.ReflectionOnlyGetAssemblies() for its presence. It's loaded alright. But when i then do assembly.GetTypes(), it dies.
Is there something i can do about this, or do i have to reflect over assemblies in a separate AppDomain and marshall the meta data back into the appdomain that has a reference to Dependency 1.0.0.0?