Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically Load Assembly and manually force path to get referenced assemblies

Tags:

c#

reflection

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?

like image 554
Yo Momma Avatar asked Dec 09 '09 14:12

Yo Momma


People also ask

Which of the following static types allow you to load an assembly directly?

Assembly type, you can get three static types which allow you to load an assembly directly: LoadFrom. LoadFrom. LoadWithPartialName.

What is the method to load assembly by name?

Loads an assembly given its AssemblyName. The assembly is loaded into the domain of the caller using the supplied evidence. Loads the assembly with a common object file format (COFF)-based image containing an emitted assembly. The assembly is loaded into the application domain of the caller.

When assembly will load on AppDomain?

If an assembly is loaded into the same AppDomain, then the class can be instantiated in the usual way. But if an assembly is loaded into a different AppDomain then it can be instantiated using reflection. Another way is an interface.

Can not load file or assembly?

In summary if you get the "Could not load file or assembly error", this means that either your projects or their references were built with a reference to a specific version of an assembly which is missing from your bin directory or GAC.


2 Answers

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.

like image 132
Tim Robinson Avatar answered Oct 14 '22 07:10

Tim Robinson


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

like image 25
SLaks Avatar answered Oct 14 '22 06:10

SLaks