We have built a plug-in application.
Program Framework. The next step is to enrich the content of the framework and provide basic services, that is, services. I think the first task is to provide plug-in management services.
Article It is also mentioned that to implement dynamic loading, you must know where the plug-in is hosted, what to load, and what not to load. These are the issues to be discussed in this article.
The first solution is where the plug-ins are placed. The traditional method I used is to place the plug-ins in the Development directory where the application is located. I will create a folder under the directory where the application is located, name it plugins. The next task is to notify which plug-ins are to be loaded and which do not need to be loaded. I will put this information in the configuration block of the application configuration file, when the application is running, it reads the configuration file and loads the plug-in based on the obtained information. In addition, our application framework is based on services, so I need to create a service for managing plug-ins.
We now define a service interface managed by plug-ins.CopySave
UsingSystem;UsingSystem. Collections. Generic;UsingSystem. text;NamespacePluginframework {Public InterfaceIpluginservice {iapplication application {Get;Set;}VoidAddplugin (string pluginname, string plugintype, string assembly, string plugindescription );VoidRemoveplugin (string pluginname); string [] getallpluginnames (); Boolean contains (string pluginname); Boolean loadplugin (string pluginname); Boolean unloadplugin (string pluginname ); iplugin getplugininstance (string pluginname );VoidLoadallplugin ();}}
The goal of pluginservice is to add/Delete the plug-in to be loaded and related information in the configuration file, and then dynamically load the plug-in. We need to define several types: in configuration block type, plugin element type, and plugin element collection type, so that we can read the plug-in information.
Finally, we implement pluginservice:CopySave
Using System; Using System. Collections. Generic; Using System. text; Using System. xml; Using System. configuration; Using System. reflection; Using System. Windows. forms; Using System. IO; Using System. collections; Namespace Pluginframework { Public Class Pluginservice: ipluginservice {Private Iapplication application = Null ; Private Pluginconfigurationsection Config = Null ; Private Dictionary <string, iplugin> plugins = New Dictionary < String , Iplugin> (); Private Xmldocument Doc = New Xmldocument (); Public Pluginservice (){} Public Pluginservice (iapplication application ){ This . Application = application;} ipluginservice members # region ipluginservice members Public Void Addplugin ( String Pluginname, String Plugintype, String Assembly, String Plugindescription) {Doc. Load (appdomain. currentdomain. setupinformation. configurationfile); xmlnode pluginnode = Doc. selectsinglenode ( "/Configuration/pluginsection" ); Xmlelement ele = Doc. createelement ( "Add" ); Xmlattribute ATTR = Doc. createattribute ( "Name" ); ATTR. value = pluginname; ELE. setattributenode (ATTR); xmlattribute attrtype = Doc. createattribute ( "Type" ); Attrtype. value = plugintype; ELE. setattributenode (attrtype); xmlattribute attrass = Doc. createattribute ( "Assembly" ); Attrass. value = assembly; ELE. setattributenode (attrass); xmlattribute attrdes = Doc. createattribute ( "Description" ); Attrdes. value = plugindescription; ELE. setattributenode (attrdes); pluginnode. appendchild (Ele); Doc. Save (appdomain. currentdomain. setupinformation. configurationfile); configurationmanager. refreshsection ( "Pluginsection" );} Public Void Removeplugin ( String Pluginname) {Doc. Load (appdomain. currentdomain. setupinformation. configurationfile); xmlnode node = Doc. selectsinglenode ( "/Configuration/pluginsection" ); Foreach (Xmlnode n In Node. childnodes ){ If (N. Attributes! =Null ){ If (N. attributes [0]. value = pluginname) {node. removechild (n) ;}} Doc. save (appdomain. currentdomain. setupinformation. configurationfile); configurationmanager. refreshsection ( "Pluginsection" );} Public String [] Getallpluginnames () {Config = (pluginconfigurationsection) configurationmanager. getsection ( "Pluginsection" ); Pluginconfigurationelement Pe = New Pluginconfigurationelement (); arraylist PS = New Arraylist (); For (Int32 I = 0; I <config. plugincollection. Count; I ++) {Pe = config. plugincollection [I]; PS. Add (PE. Name );} Return (String []) PS. toarray (Typeof (String ));} Public Bool Contains ( String Pluginname) {Config = (pluginconfigurationsection) configurationmanager. getsection ( "Pluginsection" ); Pluginconfigurationelement Pe = New Pluginconfigurationelement (); List <string> PS = New List < String > (); For (Int32 I = 0; I <config. plugincollection. Count; I ++) {Pe = config. plugincollection [I]; PS. Add (PE. Name );} Return PS. Contains (pluginname );} Public Bool Loadplugin ( String Pluginname) {boolean result = False ; Config = (pluginconfigurationsection) configurationmanager. getsection ( "Pluginsection" ); Pluginconfigurationelement Pe = New Pluginconfigurationelement (); string Path = path. getdirectoryname (system. Windows. Forms. application. executablepath) + "Plugin" ; Try { For (Int32 I = 0; I <config. plugincollection. Count; I ++) {Pe = config. plugincollection [I]; If (PE. Name = pluginname) {Assembly = assembly. LoadFile (path + """" + PE. assembly); Type type = assembly. getType (PE. type); iplugin instance = (iplugin) activator. createinstance (type); instance. application = application; instance. load (); plugins [pluginname] = instance; Result = True ; Break ;}} If (! Result) {MessageBox. Show ( "Not found the plugin" );}} Catch (Exception e) {MessageBox. Show (E. Message); Result = False ;} Return Result ;} Public Bool Unloadplugin ( String Pluginname) {boolean result = False ; Try {Iplugin plugin = getplugininstance (pluginname); plugin. Unload (); Result = True ;} Catch (Exception e) {MessageBox. Show (E. Message );} Return Result ;} Public Void Loadallplugin () {pluginconfigurationelement Pe = New Pluginconfigurationelement (); Config = (pluginconfigurationsection) configurationmanager. getsection ( "Pluginsection" ); String Path = path. getdirectoryname (system. Windows. Forms. application. executablepath) + "Plugin" ; Try { For (Int32 I = 0; I <config. plugincollection. Count; I ++) {Pe = config. plugincollection [I]; Assembly = assembly. LoadFile (path + """" + PE. assembly); Type type = assembly. getType (PE. type); iplugin instance = (iplugin) activator. createinstance (type); instance. application = application; instance. load (); plugins [PE. name] = instance ;}} Catch (Exception e) {MessageBox. Show (E. Message );}} Public Iapplication application { Get { Return Application ;} Set {Application = value ;}} Public Iplugin getplugininstance ( String Pluginname) {iplugin plugin = Null ; If (Plugins. containskey (pluginname) {plugin = plugins [pluginname];} Return Plugin ;} # Endregion }}
BecauseCodeI will not list them one by one. I will only list the more important code.Source code. After implementing pluginservice, we need to use this service to manage the plug-in. My approach is to add a project in a menu, when a user clicks this project, the plug-in management dialog box is displayed. The user selects the plug-ins in this dialog box. When the plug-in is selected, the plug-in will be loaded immediately, it is recorded in the configuration file. When you run the application next time, the plug-in is automatically loaded by default.
In addition, we need to use the configuration file from now on. Therefore, we need to add an app. config file to the application. The content of the file is as follows:CopySave
XML Version = "1.0" encoding = "UTF-8" ?> Configuration configsections section name = "pluginsection" type = "pluginframework. pluginconfigurationsection, pluginframework " /> configsections pluginsection Configuration
<textarea style="width: 99%; height: 200px; background-color: #eeefe6"><? XML version = "1.0" encoding = "UTF-8"?> <Br/> <configuration> <br/> <configsections> <br/> <section <br/> name = "pluginsection" <br/> type = "pluginframework. pluginconfigurationsection, pluginframework "<br/> </configsections> <br/> <pluginsection> <br/> </configuration></textarea>
In general, we provide a basic implementation for the management of plugin. If you have any questions, you can refer to the source code provided by me or contact me via e-mail.
Source code download
Http://files.cnblogs.com/guanjinke/pluginsample2.rar