Can a call to Assembly.Load(byte[]) raise the AppDomain.AssemblyResolve event?

前端 未结 5 1990
迷失自我
迷失自我 2020-12-25 14:05

Suppose I have a handler for AppDomain.AssemblyResolve event, and in the handler I construct a byte array and invoke the method Assembly.Load(byte[]). Can this method itself

5条回答
  •  -上瘾入骨i
    2020-12-25 14:15

    A module initializer is the only trouble-maker I can think of. A simple example of one in C++/CLI:

    #include "stdafx.h"
    #include 
    
    using namespace msclr;
    using namespace ClassLibrary10;
    
    class Init {
        gcroot 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 :)

提交回复
热议问题