【问题标题】:Get plugin that matches string c# [closed]获取与字符串 c# 匹配的插件 [关闭]
【发布时间】: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#


【解决方案1】:

使用 linq 的.Where:

frameworkPlugs = Plugins.Where(p => p.TargetFramework == framework);

你可以把它们放在一起:

public void GetPluginByTargetFramework(string framework) 
{
    Plugins.Where(p => p.TargetFramework == framework)
           .ToList().ForEach(p => p.Action());


    //Better to use a foreach loop on the items returned from the where
    foreach(var item in Plugins.Where(p => p.TargetFramework == framework)
           item.Action();
}

请注意,无需检查集合是否为空,因为 linq 查询返回一个永远不会为空的 IEnumerable&lt;T&gt; - 如果没有匹配的 where 它将为空,但不为空 em>。

如果您想比较不区分大小写的字符串,请查看:linq case insensitive (without toUpper or toLower)

【讨论】:

  • 这不会编译。另外,使用ToListForEach 浪费时间和内存。
  • @RichardSchneider - 对于编译 - 是的 - 是复制粘贴错误。对于ToList,我同意,将添加解释 - 只是决定继续使用 OP 的方式
  • 这可行,但在使用 Enviroment.Exit (1) 退出应用程序时会创建 "Error Creating Window Handle" 异常
  • @FlamingGenius - 这是与上述代码或特定问题无关的异常。不幸的是,通过后续问题进入 SO 的方法是为他们发布一个新问题。尝试解决它,如果您仍然有问题,您可以发布一个新问题来解释问题,我很乐意提供帮助:)
  • 我不认为它开始发生,直到我将它添加到应用程序中 Google 搜索说这是创建太多对象的问题?
【解决方案2】:

您需要过滤插件列表;使用Where 方法。

public void GetPluginByTargetFramework(string framework) 
{
   if (Plugins == null) return;
   foreach (var p in Plugins.Where(p => p.TargetFramework == framework))
     p.Action();
}

顺便说一句,名称与它的操作不匹配。建议改成InitForFramework

【讨论】:

    猜你喜欢
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    • 2019-07-11
    相关资源
    最近更新 更多