c# Closing an Assembly instance
When I use the Assembly.LoadFile method, I have no outside access to the file that was loaded into assembly until my program is close开发者_开发百科d. A StreamReader, on the other hand, allows me to close the stream when I am finished using it using the StreamReader.Close()
method.
Is there some way I can do the same thing when using Assembly.Load?
Thank you,
Evan
No, that's by design.
If you want to 'UnLoad' an assembly you have to load it into a separate AppDomain.
If you just want to release the file you could load it into byte[]
first and use the (deprecated) Assembly.Load(byte[] rawAssemly)
method.
Load the assembly into a new application domain using AppDomain.CreateDomain()
and use it with a new instance of the AppDomainSetup
class with its shadow copying property set to true. http://msdn.microsoft.com/en-us/library/ms404279.aspx
This will copy assemblies to be loaded into a directory, and it will load the copies instead, leaving the originals unlocked. Use AppDomain.Unload(domain)
if you want to clean up when you're finished with the assembly.
Alternatively you can use Assembly.Load(File.ReadAllBytes("path"))
.
You can load the assembly into a separate AppDomain and then unload the AppDomain - that's the only way.
You can read the DLL file into a byte[]
(call File.ReadAllBytes
), then call Assembly.Load
with the byte array.
The assembly will not be unloaded, but you'll be able to delete the file.
It's not possible. See this other post in stackoverflow. See this article.
For @BrokenGlass' solution see here.
精彩评论