2017-09-17 17 views
0

我有一個插件,系統插件和我的插件類看起來像這樣獲取匹配串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()); 
      } 
     } 
    } 
} 
+0

所以,你想從'Plugins'有一定的'framework'唯一項目? –

+3

那麼,你的_question_是什麼?它具體是什麼?你很難搞清楚什麼?請修復您的問題,以便它包含一個好的[mcve],以及詳細和清楚地說明該代碼現在做什麼以及您希望它做什麼,以及解釋您無法解決的具體問題。 –

回答

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<T>它永遠不會爲空 - 如果沒有匹配的where它將爲空,但不爲空

如果要比較字符串大小寫不敏感的,然後看看:linq case insensitive (without toUpper or toLower)

+0

這不會編譯。另外,使用'ToList'和'ForEach'浪費時間和內存。 –

+0

@RichardSchneider - 編譯 - 真 - 是一個複製粘貼錯誤。對於'ToList',我同意,將添加解釋 - 只是決定保持OP的方式 –

+0

這工作,但用'Enviroment.Exit(1)退出應用程序時創建''錯誤創建窗口句柄' – FlamingGenius

1

您需要過濾插件列表;使用Where方法。

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

順便說一句,名稱不符合它的操作。建議更改爲InitForFramework