2011-02-05 43 views

回答

1

是的。

您可以根據通用接口或標記屬性動態加載程序集。

Dynamic assembly loading

您可能需要調查複合應用程序塊(CAB)(但它確實有一個學習曲線)

0

MS在C#的Shareware Starter Kit中有一個示例和工具。從這裏下載: http://msdn.microsoft.com/en-us/library/cc533530.aspx

這有點舊了。但是,如果您正在尋找免費資源(有一些商業解決方案),這就是所有這些都不需要從頭開始整個基礎架構的存在。

0

如果您不想使用MEF,請嘗試此操作。所有的 首先定義(在庫)的接口:

namespace Plugin01 
{ 
    public class Class1 : Interfaces.IPlugin01,Interfaces.IPlugin02 
    { 
     public string Name { get { return "Plugin01.Class1"; } } 
     public string Description { get { return "Plugin01.Class1 description"; } } 
     public void Calc1() { Console.WriteLine("sono Plugin01.Class1 Calc1()"); } 
     public void Calc2() { Console.WriteLine("sono Plugin01.Class1 Calc2()"); } 
    } 

    public class Class2 : Interfaces.IPlugin01 
    { 
     public string Name { get { return "Plugin01.Class2"; } } 
     public string Description { get { return "Plugin01.Class2 description"; } } 
     public void Calc1() { Console.WriteLine("sono Plugin01.Class2 Calc1()"); } 
    } 
} 

最後:

namespace Interfaces 
{ 
    public interface IPlugin01 
    { 
     string Name { get; } 
     string Description { get; } 
     void Calc1(); 
    } 

    public interface IPlugin02 
    { 
     void Calc2(); 
    } 
} 

然後使用類實現的接口(一個或多個)寫你的插件(可能是DLL文件組件)創建您的應用程序,加載並使用你的插件:

namespace Test 
{ 
    class Program 
    { 
     /// ------------------------------------------------------------------------------    
     /// IMPORTANT: 
     /// you MUST exclude Interfaces.dll from being copied in Plugins directory, 
     /// otherwise plugins will use that and they're not recognized as using 
     /// the same IPlugin interface used in main code. 
     /// ------------------------------------------------------------------------------    
     static void Main(string[] args) 
     { 
      List<Interfaces.IPlugin01> list1 = new List<Interfaces.IPlugin01>(); 
      List<Interfaces.IPlugin01> list2 = new List<Interfaces.IPlugin01>(); 

      List<Interfaces.IPlugin01> listtot = GetDirectoryPlugins<Interfaces.IPlugin01>(@".\Plugins\"); 

      Console.WriteLine("--- 001 ---"); 
      foreach(Interfaces.IPlugin01 plugin in list1) 
       plugin.Calc1(); 

      Console.WriteLine("--- 002 ---"); 
      foreach (Interfaces.IPlugin01 plugin in list2) 
       plugin.Calc1(); 

      Console.WriteLine("--- TOT ---"); 
      foreach (Interfaces.IPlugin01 plugin in listtot) 
       plugin.Calc1(); 
      Console.ReadLine(); 
     } 

     public static List<T> GetFilePlugins<T>(string filename) 
     { 
      List<T> ret = new List<T>(); 
      if (File.Exists(filename)) 
      { 
       Assembly ass = Assembly.LoadFrom(filename); 
       foreach (Type type in ass.GetTypes()) 
       { 
        if (!type.IsClass || type.IsNotPublic) continue; 
        if (typeof(T).IsAssignableFrom(type)) 
        { 
         T plugin = (T)Activator.CreateInstance(type); 
         ret.Add(plugin); 
        } 
       } 
      } 
      return ret; 
     } 
     public static List<T> GetDirectoryPlugins<T>(string dirname) 
     { 
      /// To avoid that plugins use Interfaces.dll in their directory, 
      /// I delete the file before searching for plugins. 
      /// Not elegant perhaps, but functional. 
      string idll = Path.Combine(dirname, "Interfaces.dll"); 
      if (File.Exists(idll)) File.Delete(idll); 

      List<T> ret = new List<T>(); 
      string[] dlls = Directory.GetFiles(dirname, "*.dll"); 
      foreach (string dll in dlls) 
      { 
       List<T> dll_plugins = GetFilePlugins<T>(Path.GetFullPath(dll)); 
       ret.AddRange(dll_plugins); 
      } 
      return ret; 
     } 
    } 

只是一個評論:我的解決方案(含接口,插件和測試控制檯應用程序)編譯我的應用我n 。\ bin和插件。\ bin \ Plugins。在這兩個文件夾中部署了我的項目所依賴的Interfaces.dll這是一個嚴重的問題,請記住(閱讀代碼中的註釋)! 因此,您可以編譯您的插件,避免該問題Interfaces.dll被複制到。\ bin \ Plugins dir;但是如果你忘記了這一點,你的應用根本無法工作;所以我決定在搜索和加載插件之前強制刪除dll。

+0

這是意大利語,但是Gian Maria Ricci(微軟MVP)寫了一篇很好的文章。 這裏是鏈接[鏈接] http://dotnetmarche.org/blogs/article/archive/2006/10/30/Creare-un-programma-estendibile-mediante-plugin-_2D00_-Parte-1.aspx [/ link ] and [link] http://dotnetmarche.org/blogs/article/archive/2007/01/29/Creare-un-programma-estensibile-tramite-plugins-_2D00_-Parte-2.aspx [/ link] – Marco 2011-03-17 11:24:28

相關問題