c# - .Net 4.0 中的动态程序集加载

标签 c# .net .net-assembly unmanaged dynamic-assemblies

我的问题始于将 .Net 2.0 应用程序移动到 .Net 4.0。我必须这样做的原因是 Windows 8 默认不启用早期的 .Net 版本,我的应用程序不能要求用户启用它。

该应用程序是一个 NPAPI 插件,它通过 UnmanagedExports 使用 .Net 组件.我将其设计为低完整性应用程序,因此它必须驻留在用户的“LocalLow”目录中。

在我的应用程序中,我使用动态程序集加载机制在运行时加载多个程序集。我使用以下方法加载程序集,

MyInterface Instance;

Assembly assembly = Assembly.LoadFrom(AssemblyFile);
Type type = assembly.GetType(Identifier); // Identifier is implementing the MyInterface 
Instance = Activator.CreateInstance(type) as MyInterface;

// Do something with the Instance

将项目修改为 .Net 4.0 后,我注意到当二进制文件放在 LocalLow 目录中时插件会崩溃(它在其他地方工作)。我的下一步是用尽可能少的代码创建一个简约插件来解决问题。我注意到动态程序集加载失败,出现以下异常,

System.IO.FileLoadException: Could not load file or assembly '<assemblyPath>' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515 (COR_E_NOTSUPPORTED)) --->

System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=131738 for more information.

我尝试了以下方法来创建一个单独的域并加载程序集,但没有成功,

添加配置“loadFromRemoteSources”也不起作用。 .Net 组件似乎没有加载 .dll.config 文件。 (可能是因为 UnmanagedExporting)

我的问题是,

  • 是否可以从 LocalLow 动态加载程序集?
  • CLR 4.0 中的新 CAS 政策是否也适用于 LocalLow? 据我目前的理解,它应该只影响通过网络加载的程序集
  • 还有其他方法可以解决这个问题吗?

最佳答案

虽然它没有具体解决您的 LocalLow 问题,但如果您能够从目录中“读取文件”,您也许可以使用此处详述的“解决方法”: How can I get LabView to stop locking my .NET DLL?

关于c# - .Net 4.0 中的动态程序集加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19656738/

相关文章:

c# - c#中的双跳

c# - 使用/不安全编译的可能缺点

c# - ILMerge 程序集未正确合并

c# - LINQ Max() 函数执行速度超慢

c# - asp.net存储过程输入/输出参数

c# - Process.Start() 导致应用程序挂起

c# - 在Windows服务中通过网络访问共享文件夹

.net - Cassandra - CLI 和 CQL 之间的区别

c# - 检查加载程序集的窗口是否打开

c# - ASP.NET MultiLine TextBox - 带换行符 (\r\n) 的 MaxLength 的正则表达式验证器 - 客户端和服务器端验证的差异