Build a plug-in Application Framework (III)-Dynamic Loading

Source: Internet
Author: User
No matter how you implement plug-in applications Program The core of the Framework is dynamic loading. In other words, without dynamic loading technology, plug-in-type application frameworks do not matter. If you use Com, you can use com api to dynamically create COM objects through progid. If you use a common DLL, you need to use the Windows API function loadlibrary to dynamically load the DLL, use the getprocaddress function to obtain the function address. To use. NET technology, you need to use several static load (load, LoadFile, loadfrom) Methods of the Assembly class to dynamically load the collection.

An assembly can contain multiple types. Therefore, an assembly can contain multiple plug-ins, just like the previous one.ArticleAs long as it is derived from the iplugin interface, we acknowledge that it is a plug-in type. After the Assembly is dynamically loaded, how can we obtain the plug-in instances contained in the assembly? This requires the reflection mechanism. We need to use the gettypes static method of assembly to obtain all types contained in Assembly, traverse all types, and determine whether each type is derived from the iplugin interface. If yes, we will use the static method createinstance of activator to obtain the instance of this plug-in .. NET Dynamic Loading is just these steps. Next, I will give a simple example to illustrate dynamic loading. First of all, we declare that this example is very simple. It is purely for dynamic loading. In our real plug-in application framework, there will be a dedicated pluginservice responsible for loading and uninstalling plug-ins.

Our plug-in is located in a DLL, so we first create a class library project. Create a firstplugin class that derives from the iplugin interface and implements the methods and attributes of the interface. As the purpose of this article is to demonstrate dynamic loading, therefore, we do not provide the default implementation for the loading event of the iplugin interface. Although a warning is given during compilation, we do not need to care about it. The function of this plug-in is to create a toolstrip docked at the bottom of the main form in the application. There is a button on this toolstrip. Click this button and a MessageBox will pop up displaying "the first plugin ". Below isCode:CopySave

 Using System; Using System. Collections. Generic; Using System. text; Using Pluginframework; Using System. Windows. forms; Namespace Firstplugin { Public   Class Firstplugin: iplugin {Private Iapplication application = Null ; Private String name = "" ; Private String description = "" ; # Region iplugin members  Public Iapplication application { Get { Return Application ;} Set {Application = value ;}} Public   String Name { Get { Return Name ;} Set {Name = value ;}}Public   String Description { Get { Return Description ;} Set {Description = value ;}} Public   Void Load (){ If (Application! = Null & Amp; application. bottomtoolpanel! = Null ){ // Create a toolstrip added to the main program Toolstrip sampletoolstrip = New Toolstrip (); toolstripbutton button = New Toolstripbutton ( "Click me" ); Button. Click + = New Eventhandler (button_click); sampletoolstrip. Items. Add (button ); // Add toolstrip at the bottom of the main program Application. bottomtoolpanel. Controls. Add (sampletoolstrip );}} Void Button_click ( Object Sender, eventargs e) {MessageBox. Show ( "The first plugin" );} // Related articles mainly focus on dynamic loading, so uninstallation is not implemented.          Public   Void Unload (){ Throw   New Exception ( "The method or operation is not implemented ." );} Public   Event Eventhandler <eventargs> loading; # Endregion }}

Next, we create a Windows application project to let the main form derive from the iapplication interface and implement the methods and attributes of the iapplication interface. Then we declare one menustrip and one statusstrip, let them dock at the top and bottom of the window respectively. Next we declare four toolstrippanels, respectively. They dock at the top, bottom, and left sides. Finally, we create a toolstrip and add a button on it, when you click this button, we dynamically load the plug-in.

For ease of demonstration, we place the generated assembly in a fixed position to facilitate loading of the main program. In this example, create a sub-Folder plugins (E: "practise" pluginsample "pluginsample" bin "debug" plugins) in the folder where the application is located. DLL. The following code is dynamically loaded:CopySave

Private VoidToolstripbutton#click (ObjectSender, eventargs e ){// Dynamically load the plug-in. For convenience, I will directly give the plug-in's locationString pluginfilepath = path. getdirectoryname (application. executablepath) +"Plugins" "firstplugin. dll"; Assembly = assembly. LoadFile (pluginfilepath );// Obtain all types in assemblyType [] types = assembly. gettypes ();// Traverse all types, find the plug-in type, create a plug-in instance, and loadForeach(Type typeInTypes ){If(Type. getinterface ("Iplugin")! =Null)// Determine whether the type is derived from the iplugin Interface{Iplugin plugin = (iplugin) activator. createinstance (type );// Create a plug-in instancePlugin. Application =This; Plugin. Load ();}}}

CompleteSource codeIt is also attached for your convenience: Download the source code
Http://files.cnblogs.com/guanjinke/pluginsample.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.