C# 如何将此程序集加载到我的AppDomain?
我已经对此进行了彻底的搜索,但我不太明白为什么在文件明显存在并且可以使用file.ReadAllBytes打开时会出现FileNotFoundException 这是我当前的代码:C# 如何将此程序集加载到我的AppDomain?,c#,dll,plugins,.net-assembly,appdomain,C#,Dll,Plugins,.net Assembly,Appdomain,我已经对此进行了彻底的搜索,但我不太明白为什么在文件明显存在并且可以使用file.ReadAllBytes打开时会出现FileNotFoundException 这是我当前的代码: AppDomainSetup domainInfo = new AppDomainSetup(); domainInfo.ApplicationBase = PluginDirectory; Evidence adEvidence = AppDomain.CurrentDomain.Evidence; AppDoma
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事件处理程序中加载程序集,如所述您可以运行以检查应用程序在哪个位置搜索dll,也许您会知道为什么它不在所需位置搜索。我尝试不使用Assembly.Load,因为它会在卸载appdomain之前一直使用dll文件。我想在我所有的插件都被加载并且我需要的每个方法的MethodInfo变量都存储在一个列表中之后,立即卸载appdomain。这样我可以在以后调用这些方法。是的,但是DLL将被加载到新创建的域中,所以一旦卸载,DLL将被释放。也许我写错了:所谓“当前”域,我指的是你刚刚创建的那个域。注意,AppDomain.Load和事件处理程序都是静态的,因此只调用新创建的域。示例中的解决方案应该适用于您的目的。告诉我你是否需要一个代码示例。我刚刚注意到,你写了一篇文章,希望以后调用这些方法。。这是不可能的,因为它们加载到的域已卸载。您需要将DLL加载到某个域中,才能使用它们。您可以通过创建一个单独的域并根据需要将必要的dll加载到该域中来动态执行此操作,但我担心这样做效率不高。你到底想在这里完成什么?此外,通过在基本域中存储MethodInfo变量来引用这两个域,可能会阻止卸载。你需要找到一个不同的解决方案。