TransWikia.com

C# Assembly.Load() method loads the wrong dll?

Stack Overflow Asked by Suns Flower on November 24, 2021

Problem Statement

I’m implementing some sort of a Shadow Copy in C# when loading dll, to prevent the dll file from being locked. It works as follows:

  1. Say I have sample.dll and want to load it into the appdomain without locking it
  2. First I make a copy of the assembly with the name sample_shadow.dll in the same directory
  3. Then I load sample_shadow.dll with Assembly.Load (Assembly.LoadFrom and AssemblyName.GetAssemblyName are also tried)
  4. After that, the loaded assembly appears to have a base location of sample.dll instead of sample_shadow.dll, and sample.dll is locked

Constraints

I know I can use Assembly.Load(byte[]) or Assembly.LoadFile() to achieve this. But they can’t be used (at least I didn’t figure out how), because of a rather complicated situation:

  • I have one shell dll that dynamically loads a function dll
  • The function dll contains some static variables that is updated by the shell dll
  • There are still three more dlls that references the function dll (statically)
  • I wish the three dlls could see the updates on the static variables in the function dll made by the shell dll… (this is too much)
  • LoadFile() or Load(byte[]) breaks this behavior, but Load(string) and LoadFrom() preserve this behavior

Questions

  1. I’d like to know how can I load sample_shadow.dll correctly and thus prevent sample.dll to be locked
  2. I’d appreciate very much if someone can elaborate on the mechanisms in the background

Thanks in advance!

One Answer

You want LoadFile - LoadFrom gets assembly information from the URI then loads the known assembly. LoadFile exists explicitly for this type of use case.

"Use the LoadFile method to load and examine assemblies that have the same identity, but are located in different paths"

Answered by yugami on November 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP