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:
- Say I have sample.dll and want to load it into the appdomain without locking it
- First I make a copy of the assembly with the name sample_shadow.dll in the same directory
- Then I load sample_shadow.dll with Assembly.Load (Assembly.LoadFrom and AssemblyName.GetAssemblyName are also tried)
- 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
- I'd like to know how can I load sample_shadow.dll correctly and thus prevent sample.dll to be locked
- I'd appreciate very much if someone can elaborate on the mechanisms in the background
Thanks in advance!
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"