创建插件框架(1)
2024-07-21 02:17:13
供稿:网友
 
创建插件框架(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= @"/[email protected]/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
网站运营seo文章大全提供全面的站长运营经验及seo技术!