简体   繁体   中英

Dynamically Load Assembly and manually force path to get referenced assemblies

I am loading an assembly in C# using reflection:

Assembly = Assembly.Load([assembly_bytestream]);

The assembly being loaded references another two assemblies. To my understanding reflection will load the main assembly and then search the GAC for the referenced assemblies, if it cannot find it there, you can then incorparate an assemblyResolve event:

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
  if (args.Name.IndexOf([refAsm]) > -1)
  {
    Assembly shdocvw = Assembly.LoadFrom([dllPath]);
  }
}

The thing is, I dont want to first look in the GAC I want to force reflection to load the reference assemblies from a specific path I define. Any ideas on how to do this?

To my understanding reflection will load the main assembly and then search the GAC for the referenced assemblies

Correct, but another important detail: the framework will look in the app domain's search path before looking in the GAC. Normally the app domain search path consists of just the directory in which the main EXE is located, although you can configure your app to look in specific subdirectories too, either via app.config , or by starting a second app domain and configuring it programmatically.

Where are your referenced assemblies located relative to your app's EXE?

Edit: I always refer to Suzanne Cook's assembly load cheat sheet when debugging issues like this. The rest of her blog is full of similarly useful information.

您可以在加载需要它们之前自己加载依赖程序集。

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