Create a plugin frame (1)
Title: CREATE A Plug-in Framework
Author: Roy Osherove
Translator: Easyjoy
Source: MSDN Online, link to
Http://msdn.microsoft.com/asp.net/archive/default.aspx?pull=/library/en-us/dnaspp/html/pluginframework.asp
Summary: Describes how to add plug-in support for .NET applications and provide a framework example.
Code download: Link to
http://download.microsoft.com/download/7/2/E/72e64769-58d2-4a91-9686-a1a160bb1bc8/createpluginframework.msi
table of Contents
Why do I need a plugin framework?
Step 1: Create a simple text editor
Step 2: Create a Plug-in SDK
Step 3: Create your own custom plugin
Step 4: Let the app find new plugin
Step 5: Parlect the configuration file with IconfigurationSectionHandler
Instantiate and call the plugin
Call plugin
to sum up
Why do I need a plugin framework?
For the following reasons, people usually add plug-in support for the application:
(1) Extended functionality without the need to recompile and publish procedures>
(2) Add æ function without visiting the source code;
(3) The application business rules are frequent, or there is often a new rule to be added;
This article will construct a simple text editor that has only one window. The unique function of the program is to display a line of text in the editing box in the form. After this program, we started to create a simple plugin for it; this plugin reads the text in the edit box, resolves the valid Email address, and then returns the email.
As mentioned above, in this example we face unknown factors:
(1) How to find plugins itself;
(2) How the plugin knows the content in the text editing box;
(3) How to activate the plugin;
Step 1: Create a simple text editor
Detailed operation here is omitted. It already contains it in the downloaded source code. From this point, I assume that you have created this simple program.
Step 2: Create a Plug-in SDK
The program has now been constructed, how to make the application communicate with the outer plugin? How to make them exchange?
The solution is that the application publishes an interface, all plugins to implement public members and methods in the interface. It is assumed here that this interface is IPLUGIN, and all developers who want to develop plugins must implement this interface. This interface is located in Shared Library, the application and plugin use it. Below is the definition of the interface:
Public interface iplugin
{
String name {get;}
Void PerformAction (iPluginContext Context);
}
This code is easy to understand, but why should I give PerformAction an IPLUGINCONTEXT interface? The reason is that this phase can be delivered directly as a parameter as a parameter, and the transfer interface can have more flexibility. At present, the IPLUGINCONTEXT interface is very simple:
Public Interface ipluginContext
{
String currentdocumenttext {get; set;}}
Now what you want to do is to implement the interface in a number of objects and pass the object to the plugin and get the results. This makes it possible to modify the text box in the future, but also modify any object.
Step 3: Create your own custom plugin
What is going on now is
(1) Create a separate class library object;
(2) Create a class implementation interface iPlugin;
(3) Compile and put it in the same directory;
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 change 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 ();
}
}
Last continued
Remove some of the case http://www.9cbs.net/develop/read_article.asp?id=26339