加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 动态装配在.Net 4.0中加载

发布时间:2020-12-15 07:48:39 所属栏目:百科 来源:网络整理
导读:我的问题从将.Net 2.0应用程序移动到.Net 4.0开始.我不得不这样做的原因是 Windows 8默认情况下不启用早期的.Net版本,我的应用程序不能要求用户启用它. 该应用程序是一个NPAPI插件,它通过UnmanagedExports使用.Net组件.我将其设计为低完整性应用程序,因此它
我的问题从将.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.

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

> http://blogs.msdn.com/b/shawnfa/archive/2009/06/08/more-implicit-uses-of-cas-policy-loadfromremotesources.aspx
> http://www.west-wind.com/weblog/posts/2009/Jan/19/Assembly-Loading-across-AppDomains

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

我的问题是

>是否可以从LocalLow动态加载程序集?
> CLR 4.0中的新CAS策略是否也适用于LocalLow?从目前为止我所了解的,它只会影响通过网络加载的程序集
>还有其他办法克服这个问题吗?

解决方法

虽然它不具体解决您的LocalLow问题,如果您能够从目录“读取文件”,您可能可以使用这里详细说明的“解决方案”:
How can I get LabView to stop locking my .NET DLL?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读