I have a plugin system and my plugin class looks like this
namespace CSV_Analyzer_Pro.Core.PluginSystem
{
public interface IPlugin
{
string Name { get; }
string Version { get; }
string TargetVersion { get; }
string Description { get; }
string TargetFramework { get; }
void Action();
}
}
In my loader class I have a function that calls the Action
method of each plugin which is called when the application is loaded
public void Init()
{
if(Plugins != null)
{
Plugins.ForEach(plugin => plugin.Action());
}
}
I would like to use a similiar method so I can call in my application
loader.getByTargetFramework("UI");
This should get all plugins targeting the "UI"
framework and put them in a list and then I can iterate through the methods
This is what I have so far
public void GetPluginByTargetFramework(string framework)
{
//Get all plugins
List<IPlugin> frameworkPlugs = new List<IPlugin>();
//Put all plugins targeting framework into list
if(frameworkPlugs != null)
{
frameworkPlugs.ForEach(plugin => plugin.Action());
}
}
If it helps knowing what the different variables are here is the entire PluginLoader
class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
namespace CSV_Analyzer_Pro.Core.PluginSystem {
public class PluginLoader {
public static List<IPlugin> Plugins { set; get; }
public void LoadPlugins() {
Plugins = new List<IPlugin>();
if (Directory.Exists(Constants.PluginFolder)) {
string[] files = Directory.GetFiles(Constants.PluginFolder);
foreach(string file in files) {
if (file.EndsWith(".dll")) {
Assembly.LoadFile(Path.GetFullPath(file));
}
}
}
Type interfaceType = typeof(IPlugin);
Type[] types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()).Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass).ToArray();
foreach(Type type in types) {
Plugins.Add((IPlugin)Activator.CreateInstance(type));
}
}
public void Init() {
if(Plugins != null) {
Plugins.ForEach(plugin => plugin.Action());
}
}
public void GetPluginByTargetFramework(string framework) {
//Get all plugins
List<IPlugin> frameworkPlugs = new List<IPlugin>();
//Put all plugins targeting framework into list
if(frameworkPlugs != null) {
frameworkPlugs.ForEach(plugin => plugin.Action());
}
}
}
}