可以调用Assembly.Load(byte [])引发AppDomain.AssemblyResolve事件吗? [英] Can a call to Assembly.Load(byte[]) raise the AppDomain.AssemblyResolve event?

查看:98
本文介绍了可以调用Assembly.Load(byte [])引发AppDomain.AssemblyResolve事件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个 AppDomain.AssemblyResolve 的处理程序code> 事件,然后在处理程序中构造一个字节数组并调用方法 Assembly.Load(byte []) 。此方法本身是否可以引起 AssemblyResolve 事件再次引发,并导致我的处理程序重新进入?

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 cause the AssemblyResolve event to be raised again, and cause my handler to be re-entered?

我的问题不仅限于可以使用C#编译器生成的程序集,它们还可以包含CLR支持的简短元数据和可执行代码。

My question is not restricted only to assemblies that can be generated using C# compiler, they can contain abritrary metadata and executable code supported by the CLR.

我做了一些实验,尚未发现任何情况。我尝试加载需要其他引用的程序集,尝试将CAS属性添加到加载的程序集,而其解码将需要另一个程序集,并尝试使用模块初始化程序加载程序集(全局 .cctor 方法)。在任何情况下,我都没有观察到 AssemblyResolve 事件是从 Assembly.Load(byte [])方法内部引发的,仅当某些代码稍后尝试访问已加载程序集中的类型,方法或属性时,才会发生这种情况。但是我这里可能会丢失一些东西。

I did some experiments and haven't find any cases when it happens. I tried to load assemblies that require additional references, tried to add CAS attributes to the loaded assembly whose decoding would require another assembly, tried to load an assembly with a module initializer (global .cctor method). 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.

推荐答案

据我所知 Assembly.Load 或通过其他方式加载程序集不会执行C#编译器可以生成的任何构造函数(包括静态构造函数)。因此,您不会在常见的程序集中重新进入 AssemblyResolve

To my knowledge Assembly.Load or loading assembly by other means does not execute any constructors that can be generated by the C# compiler (including static constructors). As result you're not going to get reentrancy to AssemblyResolve on commonly found assemblies.

正如您所提到的在问题中,在 Load 调用期间不执行模块初始化程序。 CLI规范中的担保清单涵盖-摘录可在 Junfeng Zhang的模块初始化程序

As you've mentioned in the question, module initializers are not executed during the Load call. Covered in list of guarantees in CLI spec - excerpt can be found in Module Initializers by Junfeng Zhang.


B。该模块的初始化方法是在第一次访问该模块中定义的任何类型,方法或数据时或之前执行的。

B. The module’s initializer method is executed at, or sometime before, first access to any types, methods, or data defined in the module

是与之相关的SO问题,通常讨论在任何类型构造函数之前运行代码,例如在程序集加载时初始化库。请注意, .Net:加载程序集时运行代码马克·格雷夫(Marc Gravell)回答说,由于安全限制,这不可能。

There are related SO questions usually discussing "run code before any type constructors" like Initialize library on Assembly load. Note that .Net: Running code when assembly is loaded has an answer by Marc Gravell that states it may not be possible due to security constraints.

这篇关于可以调用Assembly.Load(byte [])引发AppDomain.AssemblyResolve事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆