[MEF plug-in Development] a simple example: mef plug-in development example

Source: Internet
Author: User

[MEF plug-in Development] a simple example: mef plug-in development example

I learned about this technology in my blog and learned it for a few days.

Below are some good blog posts for your reference:

MEF core notes

MEF programming guide blog Summary

 

First

1. Create a solution

Create a solution Mef and add a winform project: MefDemo

 

Open the Form1 window, pull the MenuStrip Menu Control, and change the name to "ms". The menu control does not contain any menu, for example:

 

 

 

Double-click the window and write the following code:

Private void Form1_Load (object sender, EventArgs e) {ToolStripMenuItem item = new ToolStripMenuItem (""); ms. items. add (item); foreach (IPlugin plugin plugins) {ToolStripMenuItem subItem = new ToolStripMenuItem (plugin. text); subItem. click + = (s, arg) => {plugin. do () ;}; item. dropDownItems. add (subItem );}}

  

Continue to write an initialization plug-in code:

Private CompositionContainer _ container; private void Init () {// sets the directory so that the engine can automatically discover the new extension var catalog = new AggregateCatalog (); catalog. catalogs. add (new DirectoryCatalog (AppDomain. currentDomain. baseDirectory + "plugin \"); // create a container, equivalent to the production workshop _ container = new CompositionContainer (catalog ); // call the ComposeParts in the workshop to combine each part. try {this. _ container. composeParts (this); // you only need to pass in the current application instance, and other parts will automatically discover and assemble} catch (CompositionException compositionException) {Console. writeLine (compositionException. toString ());}}

  

Add the Form1 constructor to the Init method. When the program is started, it searches for and loads all dll files from the plugin directory under the running directory. plugin is the plug-in directory where all plug-ins are stored.

public Form1()        {            InitializeComponent();            Init();        }

  

2. Create an interface class library PluginInterface

Create an interface project (PluginInterface), which is like a data cable or power cord. One End connects to a terminal (mobile phone or computer) and one end is plugged into a socket (computer USB interface or plug-in ), it is similar to the situation where a mobile phone is charged on a computer (it requires a data cable, one end connects to a mobile phone, and one end connects to a computer ).

Create an API IPlugin

Public interface IPlugin {string Text {get;} // plug-in name void Do (); // action}

  

Now return to the main program, open the Form1 window, and write the following code:

System. ComponentModel. Composition must be referenced.

[ImportMany]        public IEnumerable<IPlugin> plugins;

  

 

3. Create a plug-in Class Library

Now you can develop a plug-in. For convenience, I created a Plugin1 class library in the same solution.

Create a new class MyPlugin under the class library and implement IPlugin. Note that you need to change the MyPlugin namespace to the same as that of the main program.

Namespace MefDemo {[Export (typeof (IPlugin)] public class MyPlugin: IPlugin {public string Text {get {return "1" ;}} public void Do () {MessageBox. show (Text );}}}

  

In the same way, create two plug-in class libraries: Plugin2 and Plugin3 (winform)

After the plug-in class library is compiled, copy the dll to the main program plugin directory.

 

Download Demo

 

Related Article

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.