views:

171

answers:

1

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?

A: 

In Customizing the .NET Framework Common Language Runtime , it is explained that Load and LoadFrom create separate "scopes" for loaded assemblies, which I guess explains the problem. Something to try is to find the assemblies yourself and load them all with LoadFrom.

Have you tried handling the AppDomain.AssemblyResolve event ?

Timores
I tried both AppDomain.AssemblyResolve and AppDomain.ReflectionOnlyAssemblyResolve and neither ever fired. I assume it's because it already has a different version loaded. I'll look at whether i can use only LoadFrom.
Arne Claassen
Turns out it was a matter of using LoadFrom for loading the dependents. My initial tests kept failing but for other reasons. all good now.
Arne Claassen
On second thought, it makes sense, as there are DLLs loaded by .NET itself, which is done with Load and not LoadFrom. So, the correct suggestion should have been to try and do without LoadFrom
Timores
The problem with Load was that the other version was already loaded, so somehow Load would not fail but not load it either, but by using LoadFrom it succeeded.I actually fall back on Load if LoadFrom fails (so i can load GAC dependencies). It's just that Load gives me a false positive.
Arne Claassen