如何将此程序集加载到我的 AppDomain 中?

How to load this assembly into my AppDomain?

我已经彻底搜索过了,但是当文件明显存在并且可以用 File.ReadAllBytes

打开时,我还是不太明白为什么我会收到 FileNotFoundException

这是我当前的代码:

AppDomainSetup domainInfo = new AppDomainSetup();
domainInfo.ApplicationBase = PluginDirectory;
Evidence adEvidence = AppDomain.CurrentDomain.Evidence;
AppDomain pluginDomain = AppDomain.CreateDomain("pluginDomain", adEvidence, domainInfo);

foreach (FileInfo file in files)
{
    if (m_AssemblyIgnoreList.Contains(file.Name))
        continue;

    byte[] assemblyBytes = File.ReadAllBytes(file.FullName);
    //Assembly plugin = Assembly.LoadFrom(file.FullName);

    Assembly plugin = pluginDomain.Load(assemblyBytes);

    LoadPlayerEvents(plugin);
}

AppDomain.Unload(pluginDomain);

我正在尝试加载插件文件夹中的所有 .dll 文件,并加载一堆属性类型和函数。

当我使用 Assembly.LoadFrom(file.FullName) 时加载属性类型和函数工作文件。但是,pluginDomain.Load(assemblyBytes) 导致以下异常:

FileNotFoundException: Could not load file or assembly 'Example Mod, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

它确实找到了指定的文件,因为 File.ReadAllBytes 工作得很好,因为异常显示了我试图加载的程序集的全名。因此我得出的结论是它无法加载依赖项。

所有依赖项都已加载到 CurrentDomain 中。虽然,即使我将这些依赖项放在 .dll 旁边(这发生在构建所述 .dll 的过程中),也会产生相同的异常。

当文件明明存在时,为什么我会收到此异常?

您应该在当前域的 AssemblyResolve 事件处理程序中加载程序集,如解释的那样 here