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

I am trying to load an assembly without locking the file. These assemblies can be third-party assemblies, so we do not necessarily have access to the code, and one or two of them use Assembly.Location to read files from their directory, files that they can depend on.

I know that you can do this with Shadow Copying, but it is a real pain to make it work correctly, and several users on certain forums have recommended loading the assembly into an array of bytes and using Assembly.Load (Byte []) overloads. This works fine until one of these assemblies tries to access the 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 set the Codebase or Location properties for the assembly I am loading in any way? In the MSDN documentation for Codebase and Location, they are defined as redefinable properties - does this mean that I can redefine them from the hosting application?

+3
source share
2 answers

Can you use AppDomainSetup.ApplicationBase? Or do you need to define this path for each loadable assembly?

EDIT : The use of the file name is easy to define for the code:

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

Perhaps you could use events AppDomain.AssemblyLoador Assembly.ModuleResolve, but I doubt it.

+1
source

Using::

var assembly = Assembly.ReflectionOnlyLoad(System.IO.File.ReadAllBytes(yourFullfileNamePath));

But you still have to do it in another AppDomain.

0
source

Source: https://habr.com/ru/post/1720457/


All Articles