如何动态加载包含非托管代码的原始程序集?(绕过“无法验证的代码失败策略检查"异常) [英] How do I dynamically load raw assemblies that contains unmanaged code?(bypassing 'Unverifiable code failed policy check' exception)

查看:14
本文介绍了如何动态加载包含非托管代码的原始程序集?(绕过“无法验证的代码失败策略检查"异常)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将举一个使用 System.Data.SQLite.DLL 的例子,它是一个混合的具有非托管代码的程序集:如果我执行这个:

I'm going to give an example of using System.Data.SQLite.DLL which is a mixed assembly with unmanaged code: If I execute this :

  var assembly= Assembly.LoadFrom("System.Data.SQLite.DLL")

不会抛出异常,但如果我这样做:

No exceptions are thrown, but if I do this :

  var rawAssembly = File.ReadAllBytes("System.Data.SQLite.DLL");
  var assembly = Assembly.Load(rawAssembly);

CLR 抛出 FileLoadException 并带有无法验证的代码未能通过策略检查.(来自 HRESULT 的异常:0x80131402)".假设我正在尝试将这个程序集加载到子 AppDomain 上,如何自定义 AppDomain 的安全性以允许我通过策略检查?

The CLR throws a FileLoadException with "Unverifiable code failed policy check. (Exception from HRESULT: 0x80131402)". Let's say I'm trying to load this assembly on a child AppDomain, how can I customize the AppDomain's security to allow me pass the policy check?

推荐答案

我们是一个糟糕的异常消息的受害者.不支持使用包含非托管代码的 Assembly.Load(byte[]) 加载程序集.这是本反馈项.

We are the victim of a crummy exception message. Loading assemblies with Assembly.Load(byte[]) that contain unmanaged code is not supported. This is the subject of this feedback item.

更新:链接的反馈项消失了,作为 VS2012 发布时清理的一部分被删除.它唯一可以恢复的部分是从另一个网页复制的这个片段:

UPDATE: the linked feedback item is gone, deleted as part of the cleanup at VS2012 release time. The only part of it could still recover is this fragment, copied from another web page:

[...] 我们只允许加载 ILOnly 图像 [...] 因为其他任何东西都不安全"--

"[…] we only allow ILOnly images to be loaded […] since anything else is not safe"--

更新:链接已通过 archive.org 备份副本修复.

UPDATE: link fixed with archive.org backup copy.

这篇关于如何动态加载包含非托管代码的原始程序集?(绕过“无法验证的代码失败策略检查"异常)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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