7
votes

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?

2

2 Answers

8
votes

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.

4
votes

You could load the dependent assemblies yourself before loading the one that requires them.