Can't load a specific 32-bit assembly through Reflection


I'm using the library SharpShell to develop a simple shell extension (a property sheet) to display some information for .NET assemblies, see:

enter image description here

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=, 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=, 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:

asked on Stack Overflow Sep 9, 2018 by ElektroStudios • edited Sep 9, 2018 by ElektroStudios

1 Answer


It is not possible to load Assemblies for a different processor architecture than the current process using Assembly.Load().

Use Assembly.ReflectionOnlyLoadFrom() instead.

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_4.0.0.0__b03f5f7f11d50a3a\System.Web.dll"));

This throws the exception as described in the question.

var lAss = Assembly.ReflectionOnlyLoadFrom((@"C:\Windows\Microsoft.NET\assembly\GAC_32\System.Web\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Web.dll"));

This throws no exception and shows the message box.

answered on Stack Overflow Sep 9, 2018 by NineBerry • edited Sep 9, 2018 by NineBerry

User contributions licensed under CC BY-SA 3.0