动态装配在.Net 4.0中加载

时间:2013-10-29 11:16:15

标签: 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? 据我所知,到目前为止,它应仅影响通过网络加载的程序集
  • 还有其他办法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

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