简体   繁体   中英

How to load this assembly into my AppDomain?

I've searched the hell out of this, but I can't quite find out why I'm getting a FileNotFoundException when the file clearly exists and can be opened with File.ReadAllBytes

This is my current code:

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);

I'm attempting to load in all .dll files from within a plugin folder, and load a bunch of attributed types and functions.

The loading of the attributed types and functions works file when I use Assembly.LoadFrom(file.FullName). However, pluginDomain.Load(assemblyBytes) causes the following exception:

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.

Its definitely finding the file specified, since File.ReadAllBytes works perfectly, as the exception displays the full name of the assembly I'm trying to load. Thus I come to the conclusion that it cannot load a dependency.

All of the dependencies are already loaded in the CurrentDomain. Though, even when I place those dependencies right next to the .dll (this occurs during the build of said .dll anyways), the same exception is produced.

Why am I getting this exception, when the file clearly exists?

您应该加载组件在当前域的AssemblyResolve事件处理程序,如解释在这里

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM