Create a plugin frame (2)

zhaozj2021-02-16  47

Create a plugin frame (2)

Title: Create a Plug-In FrameWork Author: Roy Osherove Translator: easyjoy Source: MSDN Online, links 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 an example of framework.

Code download: Link to http://download.microsoft.com/download/7/2/E/72e64769-58d2-4a91-9686-a1a160bb1bc8/CreatePlugInframework.msi

Step 4: Let the app find new plugin

How do I get the app know after compiling the plugin? The solution is simple: (1) Creating an application profile; (2) Creating a one in the configuration file to list all plugins; (3) Creating a parser to resolve the item;

To complete (1), simply add an XML file to the main program.

[TIP] That is named app.config for this file, so that each compiler, Visual Studio.net automatically copies the file to the target folder and is renamed .config.

The plug-in writer can be easily added to the configuration file. Below is this profile:

Type = "Royo.pluggableApp.plugInsectionHandler, PluggableApp"

/>

Note that the Configsections tab indicates that there is a Plugins item in the application configuration, and there is a parser to parse the Plugins item. The parser is in the class Royo.pluggableApp.plugInsectionHandler, the name of the accessory is PlugGableApp. This class will be displayed in the next step.

Next, there is a PLUGINS tab in the configuration file, which lists all plugins, including class names and assembly names.

After the configuration file is written, half of the task has been completed. The rest is the application to read configuration information, and instantix the plugin.

Step 5: Parlect the configuration file with IconfigurationSectionHandler

In order to solve the plug-in information in the configuration file, the framework provides a very simple mechanism so that you can register a class as a specific part of a particular part as a configuration file. For some part in the configuration file, if the frame cannot be resolved, you need a dedicated processor; otherwise the application will throw configureException.

In order to provide the class of the Plug-INS item, you need to implement the following interface, this interface is simple, the code is as follows:

//System.configuration.iconfigurationSectionHandler, public interface iconfigurationsectionhandler {

Public Object Create (Object Parent, Object ConfigContext, System.xml.xmlNode Section);

}

What we have to do is overwrite the Create method and parse the XML node in this method. Here, the node to be parsed is the "Plugins" node. This class also needs to have a default constructor so that the framework can dynamically instantiate it. The code of the class is as follows:

Public Class PluginsectionHandler: iConfigurationSectionHandler

{

Public PlugInsectionHandler ()

{

}

// Iteerate Through all the child nodes

// of the xmlnode what was passed in and create instances

// of the specified type by Reading the attribite value of the nodes

// We use a try / catch here because some of the nodes

// might contain an invalid reference to a plugin Type

Public Object Create (Object Parent,

Object configcontext,

System.xml.xmlNode Section)

{

Plugincollection plugins = new plugincolction ();

Foreach (XMLNode Node In Section.childNodes)

{

// Code Goes here to instantiate

// and invoke the plugins

.

.

.

}

Return Plugins;

}

}

As mentioned earlier, you provide information about the framework to process the PLUG-INS item in the ConfigSECTION tab, which is before the PLUG-INS tag.

Type = "Royo.pluggableApp.plugInsectionHandler, PluggableApp"

/>

...

See how the above is specified. There are two parts of the string, one is a full name (including the namespace), a comma, and the assembly. This is the information required by the framework to instantiate a class, but also the information required to instantiate a plugin.

Instantiate and call the plugin

OK, according to the following code, how should we install a plugin? String classname = "Royo.plugins.mycustomPlugin, MyCustomPlugin"

IPlugin Plugin = (iplugin) Activator.createInstance (Type.gettype (ClassName));

What happened here: Since the application does not directly reference the assembly where the plugin is located, you must use the System.Activator class. Activator is a special class that generates an instance of an object based on any number of parameters. If you use ASP or Visual Basic, you can remember to use the createObject () function, instantiate and return to an object based on a class's CLSID. Activator is also the same idea, but uses different parameters, returning is a system.object object, not a variant. When calling Activator, the incoming parameter is a type (TYPE). Returns an instance of the type of the plugin type with the Type.getType () method. Note Type.gettype () as a parameter in the string in Plug-Ins, this parameter describes the desired class and assembly.

After instantividualization of the plug-in, forced to convert to iplugin. Here you need a try-catch block, which is to ensure that the class does implement the IPLUGIN interface. Save it after getting an interface instance and continue the next XML node. code show as below:

Public Object Create (Object Parent,

Object configcontext,

System.xml.xmlNode Section)

{

// derived from CollectionBase

Plugincollection plugins = new plugincolction ();

Foreach (XMLNode Node In Section.childNodes)

{

Try

{

// Use the activator class's 'createinstance' method

// TO TRY and CREATE An Instance of the Plugin By

// Passing in the Type Name Specified in The Attribute Value

Object PlugObject =

Activator.createInstance (Type.atType (Node.attributes ["Type"]. Value);

// Cast this to an iPlugin interface and add to the collection

Iplugin plugin = (iplugin) Plugobject;

Plugins.add (plugin);

}

Catch (Exception E)

{

// catch any Exceptions

// But Continue ITERATING for more plugins

}

}

Return Plugins;

}

Call plugin

You can call the plugin now. Don't forget to pass an iPlugInText parameter. This parameter contains the information required for the normal operation of the plugin. So you need a class to implement the interface. The code is as follows: public interface ipluginContext

{

String currentdocumenttext {get; set;}

}

Public Class EditorContext: iPluginContext

{

PRIVATE STRING M_CURRENTTEXT = String.empty;

Public EditorContext (String Currendeditortext)

{

M_CurrentText = CurrentedItoteText;

}

Public String CurrentDocumentText

{

Get {return m_currenttext;}

Set {m_currenttext = value;

}

}

The plugin can be called below: Private void ExecutePlugin (iPlugin Plugin)

{

// Create a context object to pass to the plugin

EditorContext CONTEXT = New EditorContext (txtText.Text);

// the plugin changes the text property of the context

Plugin.PerformAction (Context);

TXTTEXT.TEXT = context.currentdocumentText;

}

}

to sum up

(slightly)

【Finish】

转载请注明原文地址:https://www.9cbs.com/read-25243.html

New Post(0)