Can a call to Assembly.Load(byte[]) raise the AppDomain.AssemblyResolve event?
A module initializer is the only trouble-maker I can think of. A simple example of one in C++/CLI:
#include "stdafx.h"
#include <msclr\gcroot.h>
using namespace msclr;
using namespace ClassLibrary10;
class Init {
gcroot<ClassLibrary1::Class1^> managedObject;
public:
Init() {
managedObject = gcnew ClassLibrary1::Class1;
}
} Initializer;
The Init() constructor is invoked when the module is loaded through the module initializer, right after it initializes the C runtime. You are off the hook on this kind of code though in your specific case, Assembly.Load(byte[]) is not capable of loading mixed-mode assemblies.
That is not otherwise a restriction induced by module initializers. They were added in CLR v2.0 with the specific intention to a similar jobs like this, getting a language runtime to initialize itself before it starts executing any managed code. The odds that you run into such code should be very, very low. You'll know it when you see it :)
You mentioned -
In no case I observed the AssemblyResolve event to be raised from inside the Assembly.Load(byte[]) method, it only happened if some code later tried to access types, methods or attributes in the loaded assembly. But I can be missing something here.
The points to note here -
While executing code, if a type is referenced in code and the CLR detects that the assembly containing the type is not loaded, then it will load the assembly. Your observation is correct here.
AssemblyResolve is an event defined in AppDomain type. So this event cannot be raised from inside the Assembly.Load(byte[])
Hence if you have already registered with AssemblyResolve event on the running appdomain and call Assembly.Load(byte[]) it loads the assembly in the current domain.
Now when any type from this loaded assembly is invoked which lets say happens to be calling another type defined in some other assembly, the AppDomain will call the AssemblyResolve event to try and load that assembly.