C # Plug-in development

Source: Internet
Author: User
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) ;}}

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.