Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly.Load(Byte[]) and Assembly.Location/Assembly.Codebase

I'm trying to load an assembly without locking the file. These assemblies could be third party assemblies so we don't necessarily have access to the code and one or two of them make use of Assembly.Location to read files from their directory, files they might depend on.

I'm aware you can do this via Shadow Copying but it's a real pain getting it to work properly and several users on certain forums have recommended loading the assembly into a byte array and using the Assembly.Load(Byte[]) overload. This works great until one of those assemblies tries to access a file in its parent directory because Assembly.Location returns an empty string and Assembly.Codebase returns the location of the application loading the assembly.

Is there anything I can do to somehow set the Codebase or Location properties of the assembly I'm loading? In the MSDN documentation for Codebase and Location they're defined as overridable properties - does that mean I can override them from the hosting application?

like image 241
Andy E Avatar asked Jan 24 '26 17:01

Andy E


1 Answers

Can you use AppDomainSetup.ApplicationBase ? Or do you need to define that path for every assembly you load?

EDIT: using a filename is easy to define a codebase:

AssemblyName assemblyRef = new AssemblyName();
assemblyRef.CodeBase = assemblyFile;
Assembly assembly = Assembly.Load(assemblyRef);

Maybe you could use AppDomain.AssemblyLoad or Assembly.ModuleResolve events, but I doubt.

like image 134
Rubens Farias Avatar answered Jan 26 '26 10:01

Rubens Farias



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!