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?
Assembly type, you can get three static types which allow you to load an assembly directly: LoadFrom. LoadFrom. LoadWithPartialName.
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.
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.
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.
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.
You could load the dependent assemblies yourself before loading the one that requires them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With