I'm using the library SharpShell to develop a simple shell extension (a property sheet) to display some information for .NET assemblies, see:
This shell extension and SharpShell itself is not relevant to this question, but to explain my scenario so you can understand better my question.
My project is compiled as Any CPU mode, then, to manage the loading of x86, x64 an Any CPU assemblies in my program I'm using the overload of the function
Assembly.Load() that takes the bytes of a raw assembly as unique argument:
Assembly asm = Assembly.Load( File.ReadAllBytes(filepath) );
I tested it with dlls and executables of both x86 and x64 architectures, and Any CPU, it works fine... except for the problem I'll explain.
The problem is that for some reason that I ignore it is not working for some x86 assemblies, for example when I try to load the x86 assembly: System.Web.dll (
System.Web, Version=220.127.116.11, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a ), the
Assembly.Load() function throws an exception. It is NOT a
BadImageException, its a
FileLoadException with this error message:
Could not load file or assembly 'System.Web, Version=18.104.22.168, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
( note that
Assembly.ReflectionOnlyLoad() will also throw the same exception. )
However, I can load the x64 version of the same assembly with success. Please note that as I said, I can load most of the 32-bit assemblies in my program.
If I compile my project as x86, then I can load this assembly with sucess. But my project must compile as Any CPU (to be able handle the loading of both x86 and x64 assemblies ...since I'm not aware of a better approach).
I would like to know why I can't load that specific assembly, and a viable approach to solve this issue.
Here you can download the dlls if you want to test it:
It is not possible to load Assemblies for a different processor architecture than the current process using
In a simple demo application that is run as 64 bit, I see the following behaviour:
var lAss = Assembly.Load(File.ReadAllBytes(@"C:\Windows\Microsoft.NET\assembly\GAC_32\System.Web\v4.0_22.214.171.124__b03f5f7f11d50a3a\System.Web.dll")); MessageBox.Show(lAss.Location);
This throws the exception as described in the question.
var lAss = Assembly.ReflectionOnlyLoadFrom((@"C:\Windows\Microsoft.NET\assembly\GAC_32\System.Web\v4.0_126.96.36.199__b03f5f7f11d50a3a\System.Web.dll")); MessageBox.Show(lAss.Location);
This throws no exception and shows the message box.
User contributions licensed under CC BY-SA 3.0