K
k7s41gx
Guest
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);
}
}
}
Continue reading...
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);
}
}
}
Continue reading...