MEF Host application

Source: Internet
Author: User

Host MEF in the Application

 

Managed extensibility framework (MEF) is an extension management framework on the. NET platform. It has the following features:

1. A Scalable row management framework under. net, including dependency injection and duck typing.
2. easily cope with Application Extensions and minimize the impact on existing code.
3. There is no direct dependency between applications and extensions. Multiple extension programs share the same requirements.

 

By referring to the tutorial above on the http://mef.codeplex.com, I tried to implement an example

The implementation steps are as follows:
1. Create a WPF console application. The host is the program class.
2. Add the system. componentmodel. Composition assembly and related references.
3. Add a compone () method to create a combined container
4. Add a method and call the compone () method.
5. instantiate the host in the main method
6. Define the parts exported in the host
7. Export parts in the host
8. add parts to the container in two ways.
1. Add a component assembly instance directly
2. Add components by directory

 

Implementation Code:

Using system; using system. componentmodel. composition; using system. componentmodel. composition. hosting; namespace mefconsole {// <summary> // host /// </Summary> class program {static void main (string [] ARGs) {program P = new program (); p. run1 (); p. run2 () ;}/// <summary> /// import /// </Summary> [import] public iwaiter waiter {Get; set ;} /// <summary> /// instantiate the combined container /// Add the component directly /// </Summary> private Vo Id compone1 () {var Container = new compositioncontainer (); container. composeparts (this, new waiter () ;}/// <summary> /// test the add part method directly /// </Summary> Public void run1 () {console. writeline ("add parts directly to container mode:"); compone1 (); waiter. welcome (); waiter. serve ("sweet and sour tenderloin"); waiter. goodbye (); console. writeline (); // console. read () ;}/// <summary> /// instantiate the composite container class /// Add a part through the directory /// </Summary> private void compone2 () {va R catalog = new assemblycatalog (system. reflection. assembly. getexecutingassembly (); var container2 = new compositioncontainer (Catalog); container2.composeparts (this );} /// <summary> /// Add the control through the directory for testing /// </Summary> Public void run2 () {console. writeline ("how to add parts to a container through a directory:"); compone2 (); waiter. welcome (); waiter. serve ("sweet and sour tenderloin"); waiter. goodbye (); console. read () ;}/// <summary> // declare the waiter interface /// </Summary> Public interface iwaiter {void welcome (); void serve (string name); void goodbye ();} /// <summary> /// waiter class /// mark as export /// </Summary> [Export (typeof (iwaiter)] public class waiter: iwaiter {public void welcome () {console. writeline ("Welcome! ");} Public void serve (string name) {console. writeline (" your dish "+ name +" is coming. Please use it slowly! ");} Public void goodbye () {console. writeline (" welcome to the next visit! ");}}}

Execution result:

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.