C # Extensible Programming MEF Learning Note (i): MEF Introduction and Simple Demo

Source: Internet
Author: User

Before the article begins, let's start with a brief introduction to what is MEF,MEF, the full name Managed Extensibility Framework (managed extensible framework). Single from name we are not hard to find: MEF is a framework dedicated to solving extensibility issues, as described in the MSDN section on MEF:

The Managed extensibility Framework or MEF is a library for creating scalable, lightweight applications. Application developers can use the library to discover and use extensions without having to configure them. Extension developers can also use the library to easily encapsulate code, avoiding the need to generate fragile hard dependencies. With MEF, you can reuse extensions not only within an application, but also between applications.

Don't say much nonsense, want to know more about the MEF content, to Baidu above check it!

MEF is widely used in WinForm, WPF, Win32, Silverlight, and we start with the console and see how the MEF is implemented under the console, creating a new Win32 Console project MEFDEMO, Add a Ibookservice interface and write a simple demo:

Ibookservice content is as follows:

Using system;using system.collections.generic;using system.linq;using system.text;namespace MEFDemo{public   Interface Ibookservice   {      string bookname {get; set;}      String Getbookname ();}   }

The following adds a reference to the System.ComponentModel.Composition namespace, because the DLL is not referenced in the console program, so add it manually:

Click OK to complete the Add reference, create a new class, such as: Musicbook inherits the Ibookservice interface, the code is as follows:

Using system;using system.collections.generic;using system.linq;using system.text;using System.componentmodel.composition;namespace mefdemo{   [Export (typeof (Ibookservice))] public   class Musicbook:ibookservice   {public      string BookName {get; set;}      public string Getbookname ()      {         return ' Musicbook ';      }}   }

Export (typeof (Ibookservice)) Exports the class declaration to the Ibookservice interface type.

Then modify the code in Porgram as follows:

Using system;using system.collections.generic;using system.linq;using system.text;using System.Reflection;using System.componentmodel.composition;using System.componentmodel.composition.hosting;namespace MEFDemo{   class Program   {      [Import] public      ibookservice Service {get; set;}      static void Main (string[] args)      {program         Pro = new program ();         Pro.compose ();         if (pro. Service! = null)         {            Console.WriteLine (pro. Service.getbookname ());         }         Console.read ();      }      private void Compose ()      {         var catalog = new Assemblycatalog (assembly.getexecutingassembly ());         Compositioncontainer container = new Compositioncontainer (catalog);         Container.composeparts (this);}}}   

Here, using [import] to import the Musicbook just exported, the following compose method, instantiate Compositioncontainer to implement the combination, click F5 Run, the results are as follows:

You can see the Getbookname method that called the Musicbook class, but we don't instantiate the Musicbook class, is it amazing???

This is the realization of the main program and the decoupling between classes, greatly improving the code extensibility and ease of maintenance!

C # Extensible Programming MEF Learning Note (i): MEF Introduction and Simple Demo (RPM)

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.