I have always wanted to learn plug-in programming. The main reason is that my current customer needs are changing. If I integrate all the functions into an execution file, it is inconvenient to modify and upgrade, to use plug-in programming, you only need to build Program After the framework, each time a function is completed, the user can review it and convert it to zero, so that both the development team and the customer can grasp the progress of project development. at the same time, you can use this method to enhance your confidence in project completion on time.
The following materials are mainly obtained from the Haha blog. I have found many similar materials on the Internet. I think this is the best choice for beginners.CopySave
// 1 Define the plug-in interface and compile it into a DLL, for example:UsingSystem;NamespacePlugininterface {Public InterfaceIshow {StringShow ();}}// 2 compile the plug-in. Create a DLL project and reference the DLL plug-in step 1 to implement its interface, for example:NamespacePlugina {Public ClassPlugina: plugininterface. ishow {Public StringShow (){Return "I am plugin";}}}
Collection assembly:CopySave
// 3. Find the DLL file in the specified directoryPrivate VoidFrmmain_load (ObjectSender, system. eventargs e ){// Obtain all DLL files in the Plugins directory and save them in combo.Try{StringPath = application. startuppath; Path = system. Io. Path. Combine (path,"Plugins");Foreach(StringFileInSystem. Io. Directory. getfiles (path,"*. Dll")){This. Cmbplugins. Items. Add (File );}}Catch(Exception ex) {MessageBox. Show (ex. Message );}}
Use plug-insCopySave
private void btnexecute_click ( Object sender, system. eventargs e) { try { // 1. obtain the file name string asmfile = This . cmbplugins. text; string asmname = system. io. path. getfilenamewithoutextension (asmfile); If (asmfile! = string . empty) { // 2. use reflection to construct an instance of the DLL file system. reflection. assembly ASM = system. reflection. assembly. loadfrom (asmfile); // 3. use reflection to extract classes from the Assembly (DLL) and instantiate these classes. plugininterface. ishow = (plugininterface. ishow) system. activator. createinstance (ASM. getType (asmname + "namespace. " + asmname + " class "); // 4. use the instance of this class in the main program This . label2.text = ishow. show () ;}< strong> catch (exception ex) {MessageBox. show (ex. message) ;}}