从插件加载程序集

Load an assembly from plugin

简介

我正在为 Navisworks 编写一个插件,我正在使用 Dropbox api 从存储库中 download/upload 文档。

问题

Dropbox.Api 使用 Newtonsoft.Json.dll 版本 7.0,问题是 Navisworks 使用 4.0 版本的 same 程序集,所以我无法使用 Dropbox api因为每次都会抛出异常:

System.AggregateException: One or more errors occurred. ---> System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=7.0.0.0, ...

所以据我所知,该程序具有 4.0v 程序集,因此 Dropbox.Api 无法正常执行。

到目前为止,我所做的是使用另一个进程,我可以从那里加载正确的程序集和 download/upload 文件,但我想避免使用第二个进程。

我正在尝试使用反射在运行时加载程序集,但是没有效果,程序仍然找不到更新的程序集。

//Load the assembly at the beginning of the plugin
var ass = System.Reflection.Assembly.Load(Properties.Resources.Newtonsoft_Json);

//Use the Dropbox api
//Exception...

我能否以某种方式强制程序使用较新的程序集(临时)?

是否有我遗漏的解决方案?

您遇到此问题是因为您无法加载两个不同的 non-strong-named 版本的 .NET 程序集 (无论它在文件系统上的什么位置)到同一个 AppDomain 中。默认情况下,您从一个名为 Primary AppDomain 的 AppDomain 开始。

A strong-named 程序集是一个采用文件名的程序集;版本;签署密钥和文化以生成独特的程序集。

By now, what I've done is to use another process which I can load the right assembly and download/upload the files from there, but I would like to avoid using a second process.

不需要创建第二个进程,您可以在同一个进程中创建一个第二个AppDomain。每个 AppDomain 可以加载不同版本的程序集,包括 Newtonsoft.Json 而不会冲突。

I'm trying to use reflection to load the assembly at runtime, but it takes no effect, the program still cannot find the newer assembly.

那是行不通的,这与让 .NET 自动为您做这件事本质上是一样的。

唯一可以将多个版本的 .NET 程序集加载到同一个 AppDomain 的情况是程序集(在本例中为 NuGet 包)和依赖程序集是 所有 strong-named。出于某种原因,我一直无法理解为什么大多数开源 .NET 开发人员拒绝 strong-name 程序集。