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

前端 未结 5 1988
迷失自我
迷失自我 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条回答
  •  攒了一身酷
    2020-12-25 14:26

    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 -

    1. 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.

    2. 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.

提交回复
热议问题