** 创建插件框架(1) ** ** **
标题: Create a Plug-In FrameWork
作者: Roy Osherove
译者:easyjoy
出处: MSDN Online ,链接为
摘要:介绍如何为 .Net 应用程序添加插件支持,并提供一个框架例子。
代码下载:链接为
** 目录 ** ** **
应用程序为什么需要插件框架
第一步:创建简单文本编辑器
第二步:创建 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