C# .NET4.0中的动态程序集加载

C# .NET4.0中的动态程序集加载,c#,.net,.net-assembly,unmanaged,dynamic-assemblies,C#,.net,.net Assembly,Unmanaged,Dynamic Assemblies,我的问题始于将.NET2.0应用程序移动到.NET4.0。我不得不这样做的原因是Windows 8默认情况下不启用早期的.Net版本,并且我的应用程序无法要求用户启用它 该应用程序是一个NPAPI插件,通过使用.Net组件。我将其设计为一个低完整性应用程序,因此它必须驻留在用户的LocalLow目录中 在我的应用程序中,我使用动态程序集加载机制在运行时加载多个程序集。我使用以下方法加载程序集 MyInterface Instance; Assembly assembly = Assembly.

我的问题始于将.NET2.0应用程序移动到.NET4.0。我不得不这样做的原因是Windows 8默认情况下不启用早期的.Net版本,并且我的应用程序无法要求用户启用它

该应用程序是一个NPAPI插件,通过使用.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
将项目修改为.NET4.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.
System.IO.FileLoadException:无法加载文件或程序集'
  • 添加配置“loadFromRemoteSources”也不起作用。.Net组件似乎没有加载.dll.config文件。(可能是由于未管理数据传输)

    我的问题是,

    • 是否可以从LocalLow动态加载程序集
    • CLR 4.0中的新CAS策略是否也适用于LocalLow?据我目前所知,它应该只影响通过网络加载的程序集
    • 有没有其他办法克服这个问题

    虽然它没有专门解决您的LocalLow问题,但如果您能够从目录中“读取文件”,则可以使用此处详述的“解决方法”:

    您为什么在LocalLow?这是一个目录,它被专门保护为可由不受信任的应用程序(低完整性进程)写入。它可能被视为“网络位置”,因为它用于Internet Explorer和其他浏览器,但它可能只是dll上的一个MOTW。我必须切换到LocalLow的原因是,在运行插件时,Internet Explorer会阻止对所有其他路径的访问,特别是当我们写入文件时。唯一的选择是使用LocalLow.com,这正是我的观点。由于它可以写入插件,.Net将其从加载程序集的过程中列入黑名单。LocalLow用于需要由插件编写的文件,而不是插件本身。在另一个目录中找到插件(想到程序文件)并将临时数据文件写入LocalLow。