【发布时间】:2017-09-17 05:34:27
【问题描述】:
我有一个插件系统,我的插件类看起来像这样
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的项目? -
那么,您的问题是什么? 具体而言是什么让您难以弄清楚?请修正您的问题,以便它包含一个好的minimal reproducible example,以及对该代码现在的确切作用以及您希望它做什么的详细而清晰的解释,以及对具体问题的解释你解决不了。
标签: c#