如何将我的应用程序分离到不同的模块?最终用户将只使用他们购买的模块。我可以使用插件和插件吗?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. Windows窗体应用程序性能
- 4. 如何确保Windows窗体应用程序的数据库安全性?
- 5. Mergesort C++ Windows窗体应用程序
- 6. C#Windows窗体应用程序关闭
- 7. Windows窗体应用程序
- 8. Windows窗体应用程序C#:添加全局变量:
- 9. Windows窗体应用程序中的WCF
- 10. Windows窗体应用程序中的SQLException
- 11. Windows窗体应用程序中的Windows.Web.Http?
- 12. Windows窗体应用程序中的全屏界面
- 13. c#窗体窗体应用程序
- 14. C#窗体窗体应用程序
- 15. 如何在C/C++应用程序中使用Windows窗体?
- 16. iPhone应用程序中的安全性
- 17. 在Windows窗体应用程序中使用C#的WebBrowser控件#
- 18. 线程安全调用窗体冻结应用程序
- 19. 在Windows窗体应用程序中使用OpenGL在Visual C++中
- 20. 在C#中的应用程序的内存问题Windows窗体
- 21. 带Web应用程序的Windows窗体
- 22. Windows窗体应用程序的定位
- 23. 如何锁定/解锁C#中的Windows应用程序窗体#
- 24. 从C类中的类库访问Windows应用程序窗体#
- 25. C# - 获取Windows窗体应用程序中SelectedItem的值
- 26. Windows上的安全应用程序?
- 27. 在C#中安装Windows应用程序窗体后更改应用程序配置文件中的值
- 28. 全球化现有的Windows窗体应用程序?
- 29. Android应用程序中的模块性
- 30. 在C#中使用`dotnet new`创建Windows窗体应用程序
即插即用意味着完全不同的东西。编写类库。 – 2011-02-05 14:29:55