|
| |
名片设计 CorelDRAW Illustrator AuotoCAD Painter 其他软件 Photoshop Fireworks Flash |
|
创建插件框架(1) 标题:Create a Plug-In FrameWork 作者:Roy Osherove 译者:easyjoy 出处:MSDN Online,链接为 http://msdn.microsoft.com/asp.net/archive/default.aspx?pull=/library/en-us/dnaspp/html/pluginframework.asp 摘要:介绍如何为.Net应用程序添加插件支持,并提供一个框架例子。 代码下载:链接为 http://download.microsoft.com/download/7/2/e/72e64769-58d2-4a91-9686-a1a160bb1bc8/CreatePluginFramework.msi 目录 应用程序为什么需要插件框架 第一步:创建简朴文本编辑器 第二步:创建Plug-In SDK 第三步:创建自己定制的插件 第四步:让应用程序找到新插件 第五步:用IConfigurationSectionHandler解析配置文件 实例化并调用插件 调用插件 总结 应用程序为什么需要插件框架 出于以下原因,人们通常为应用程序添加插件支持: (1)在不需要重新编译和发布程序的情况下扩展功能; (2)在不访问源代码的情况下添加新功能; (3)应用程序业务规则变化频繁,或者常常有新的规则要加进来; 本文将构造一个简朴的文本编辑器,该编辑器只有一个窗口。该程序唯一的功能就是在窗体中间的编辑框中显示一行文本。这个程序编好后,我们开始为它创建一个简朴插件;这个插件读出编辑框中的文本,解析出其中的有效的EMail地址,然后返回该EMail。 如上所述,,这个例子中我们面临如下未知因素: (1)应用程序本身如何找到插件; (2)插件如何知道文本编辑框中的内容; (3)如何激活插件; 第一步:创建简朴文本编辑器 具体操作此处省略。下载的源代码中已经包含它。从此处开始,我假设你已经创建了这个简朴程序。 第二步:创建Plug-In SDK 现在已经构造了程序,如何让应用程序与外边插件交流呢?怎样让它们交流呢? 解决方式是应用程序发布一个接口,所有的插件都要实现接口中的公共成员和方式。此处假设这个接口是IPlugin,所有想开发插件的开发人员都必须实现这个接口。这个接口位于共享库(shared library)中,应用程序和插件都用到它。下面是接口的定义: public interface IPlugin { string Name{get;} void PerformAction(IPluginContext context); } 这段代码很轻易懂,但是为什么要给PerformAction一个IPluginContext接口呢?原因在于这相对于直接把字符串当作参数进行传递,传递接口可以有更多的灵活性。目前,IPluginContext接口异常简朴: public interface IPluginContext { string CurrentDocumentText{get;set;} } 现在所要做的就是在若干对象中实现接口,并把对象传递给插件,并获取结果。这使得将来可以不仅修改文本框,还可以修改任何对象。 第三步:创建自己定制的插件 现在要做的是 (1)创建一个单独的类库对象; (2)创建一个类实现接口IPlugin; (3)编译并放到主程序同一目录下; public class EmailPlugin:IPlugin { public EmailPlugin() { } // The single point of entry to our plugin // Acepts an IPluginContext object // which holds the current // context of the running editor. // It then parses the text found inside the editor // and changes it to reflect any // email addresses that are found. public void PerformAction(IPluginContext context) { context.CurrentDocumentText= ParseEmails(context.CurrentDocumentText); } // The name of the plugin as it will appear // under the editor\\\'s "Plugins" menu public string Name { get { return "Email Parsing Plugin"; } } // Parse the given string for any emails using the Regex Class // and return a string containing only email addresses private string ParseEmails(string text) { const string emailPattern= @"/w+@/w+/./w+((/./w+)*)?"; MatchCollection emails = Regex.Matches(text,emailPattern, RegexOptions.IgnoreCase); StringBuilder emailString = new StringBuilder(); foreach(Match email in emails) { emailString.Append(email.Value + Environment.NewLine); } return emailString.ToString(); } } 未完待续 剩下部分参见http://www.csdn.net/Develop/read_article.asp?id=26339 返回类别: 教程 上一教程: asp+中的hash表操作 下一教程: VS.NET下水晶报表分发时的问题及解决2 您可以阅读与"创建插件框架(1)"相关的教程: · 创建插件框架(2) · ASP.NET创建XML Web服务全接触(12) · 第十五章 接口[《.net框架程序设计》读书笔记] · 创建ASP.NET监视服务器进程 · 创建基于ASP.NET的SMTP邮件服务 |
| 快精灵印艺坊 版权所有 |
首页 |
||