我有一個插件,系統插件和我的插件類看起來像這樣獲取匹配串c#
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();
}
}
在我的裝載機類我有一個調用每個插件的Action
方法被稱爲當應用程序的功能加載
public void Init()
{
if(Plugins != null)
{
Plugins.ForEach(plugin => plugin.Action());
}
}
我想用一個類似的方法,所以我可以在我的應用程序調用
loader.getByTargetFramework("UI");
這應該讓所有的插件針對"UI"
框架,並把它們放在一個列表,然後我可以通過這些方法迭代
這是我迄今爲止
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());
}
}
如果它有助於瞭解什麼不同的變量在這裏是整個PluginLoader
類
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());
}
}
}
}
所以,你想從'Plugins'有一定的'framework'唯一項目? –
那麼,你的_question_是什麼?它具體是什麼?你很難搞清楚什麼?請修復您的問題,以便它包含一個好的[mcve],以及詳細和清楚地說明該代碼現在做什麼以及您希望它做什麼,以及解釋您無法解決的具體問題。 –