Answered by:
Help with removing a created instance / dynamic assembly

Question
-
How do I go about removing the instance created by activator.createinstance? I'm trying to have a dynamic plugin library that can be reloaded and updated during run time. As of now I believe it is dynamic, but how do I unload the assembly? This is the plugin loader script:
public class PluginLoader { public static List<IPlugin> Plugins { get; set; } public void LoadPlugins() { Plugins = new List<IPlugin>(); //Load the DLLs from the Plugins directory if (Directory.Exists(Constants.FolderName)) { string[] files = Directory.GetFiles(Constants.FolderName); foreach (string file in files) { if (file.EndsWith(".dll")) { //Assembly.LoadFile(Path.GetFullPath(file)); Assembly.Load(File.ReadAllBytes(Path.GetFullPath(file))); } } } Type interfaceType = typeof(IPlugin); //Fetch all types that implement the interface IPlugin and are a class Type[] types = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(a => a.GetTypes()) .Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass) .ToArray(); foreach (Type type in types) { //Create a new instance of all found types Plugins.Add((IPlugin)Activator.CreateInstance(type)); } } }
Example plugin:
namespace TestPlugin { public class TestPlug : IPlugin { public string Explanation { get { return "Tests functionality of the assembly reference."; } } public string Name { get { return "TestPlug"; } } public void Go(string parameters) { string line = parameters; SYSAdmin.Program.form1.output.AppendText(Environment.NewLine); SYSAdmin.Program.form1.output.AppendText(line); } } }
- Moved by Jack J JunMicrosoft contingent staff Friday, May 1, 2020 8:33 AM
Thursday, April 30, 2020 1:21 PM
Answers
-
but how do I unload the assembly?
Unfortunately, this is more complex than you think. Once an assembly is loaded into an AppDomain, it cannot be unloaded. So you need to create a new AppDomain, load your plugin in the new AppDomain, use it, and then unload the whole AppDomain when you no longer need the assembly.
This introduces additional complications, because in order to invoke the methods in the plugin you will need to make cross-appdomain calls.
https://docs.microsoft.com/en-us/dotnet/api/system.appdomain?view=netcore-3.1
- Edited by Alberto PoblacionMVP Thursday, April 30, 2020 9:01 PM
- Marked as answer by k7s41gx Thursday, June 4, 2020 1:33 AM
Thursday, April 30, 2020 9:00 PM
All replies
-
but how do I unload the assembly?
Unfortunately, this is more complex than you think. Once an assembly is loaded into an AppDomain, it cannot be unloaded. So you need to create a new AppDomain, load your plugin in the new AppDomain, use it, and then unload the whole AppDomain when you no longer need the assembly.
This introduces additional complications, because in order to invoke the methods in the plugin you will need to make cross-appdomain calls.
https://docs.microsoft.com/en-us/dotnet/api/system.appdomain?view=netcore-3.1
- Edited by Alberto PoblacionMVP Thursday, April 30, 2020 9:01 PM
- Marked as answer by k7s41gx Thursday, June 4, 2020 1:33 AM
Thursday, April 30, 2020 9:00 PM -
Hi k7s41gx,
Thank you for posting here.
Since this thread is related to Plugin, c# forum only support c# related problem.
I suggest that you ask your question in Plugin related forum.
The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.
Thanks for understanding.
Best Regards,
Jack
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Friday, May 1, 2020 8:32 AM -
Thanks for your response Alberto. I ended up going the appdomain route to dynamically load or unload class files.Thursday, June 4, 2020 1:33 AM
-
Jack,
I am confused on what you are stating here. I am writing a program in visual c# that uses class library (.dll) files. My question pertained to the language itself, specifically how to unload a class library from an already running application.
Thanks,
-Jake
Thursday, June 4, 2020 1:36 AM