如何將我的應用程序分離到不同的模塊?最終用戶將只使用他們購買的模塊。我可以使用插件和插件嗎?C#Windows窗體應用程序中的模塊安全性?
回答
,你可以看看到Managed Extensibility Framework動態添加模塊。
MS在C#的Shareware Starter Kit中有一個示例和工具。從這裏下載: http://msdn.microsoft.com/en-us/library/cc533530.aspx
這有點舊了。但是,如果您正在尋找免費資源(有一些商業解決方案),這就是所有這些都不需要從頭開始整個基礎架構的存在。
如果您不想使用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。
這是意大利語,但是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
- 1. 使用VB.NET的Windows窗體應用程序中基於控件的安全性
- 2. Windows窗體應用程序C#安裝程序項目錯誤
- 3. 使用C#Windows窗體應用程序
- 4. Windows窗體應用程序性能
- 5. 如何確保Windows窗體應用程序的數據庫安全性?
- 6. 創建Windows窗體C#應用程序
- 7. C#Windows窗體應用程序關閉
- 8. Mergesort C++ Windows窗體應用程序
- 9. 在C++中打開新窗體Windows窗體應用程序
- 10. 如何在Windows窗體中應用安全性?
- 11. Windows窗體應用程序
- 12. Windows窗體應用程序C#:添加全局變量:
- 13. 從Windows窗體應用程序C控制檯應用程序#
- 14. Windows窗體應用程序中的Windows.Web.Http?
- 15. Windows窗體應用程序中的WCF
- 16. Windows窗體應用程序中的SQLException
- 17. C#窗體窗體應用程序音量滑塊
- 18. Windows窗體應用程序中的全屏界面
- 19. c#窗體窗體應用程序
- 20. C#窗體窗體應用程序
- 21. C#Windows窗體應用程序文本框變得模糊
- 22. 如何在C/C++應用程序中使用Windows窗體?
- 23. 在命令行上運行的線程安全Windows窗體應用程序
- 24. Windows窗體:線程安全訪問GUI?
- 25. iPhone應用程序中的安全性
- 26. 在Windows窗體應用程序中使用C#的WebBrowser控件#
- 27. 線程安全調用窗體凍結應用程序
- 28. 在Windows窗體應用程序中使用OpenGL在Visual C++中
- 29. 處理多層Windows應用程序中的安全性
- 30. 在C#中的應用程序的內存問題Windows窗體
即插即用意味着完全不同的東西。編寫類庫。 – 2011-02-05 14:29:55