Vincent Vincent - 3 months ago 14
Python Question

Python for .NET - Remove DLL reference

I am currently using Python for .NET to call methods from a C# DLL. Here is my code :

# Common Language Runtime
import clr
# Import DLL
clr.AddReference("MyDLL")
# Import class
from MyNamespace import MyClass
# Instantiation
my_instance = MyClass()
# Call methods
my_instance.myMethod()


Now, I'd like to unload and remove the DLL reference after I'm done with my instance so I can do whatever I have to do with the DLL file (like regenerate it). I've read the Python for .NET documentation but I couldn't find anything about this issue.

Answer

Here's a similar question that was posted on SO. Jon seems to echo what I said - you cannot remove a single reference from the context (AppDomain).

I ran into a code segment online that uses this piece of code:

clr.ClearProfilerData() #unload the .dll file so the file lock is released

I'm assuming you want to unload the DLL because you want to recompile it but the compiler is complaining a process is still using it? Perhaps using the method above will release it?

EDIT: Just as a follow-up, if you wanted to research into it more, try out this idea. That is, create an instance of Python in C#, load it in a custom AppDomain, and unload the custom AppDomain when finished. :)