MEF Primer superficial understanding, but try to be simple and clear (a)

Source: Internet
Author: User

Cause: Work needs to be handled differently for different types of files. For example, TXT file, direct print, doc file, directly send email,jpg file, upload xxx album site.

In fact, this problem in the study of the most basic factory model has been solved, a little bit of object-oriented, can write such a file manager. Then there is a new type of file processing, only need to add an implementation class, and then in the factory add an if judgment, return a specific processing instance, the upper layer does not have to change.

To get rid of the If inside the factory, you have to call out the IOC container. MEF is Microsoft's own managed extensible framework, which I used as an IOC container, other functions, superficial understanding.

We first use MEF to practice practiced hand and then apply it to specific projects. The concept of something to throw aside first.

Create a new console Application project and a class library project with two projects referencing the MEF library. A new three class file is created in the Class Library project.

 using   System;  using   System.ComponentModel.Composition;  namespace   parts{[Export ( typeof  (object )]//  indicates that this class needs to be exported and the exported type is Object  public  class   Txtfilehandler { public  void   Process () {Console.WriteLine (   processing the text file   " ); }    }}

The rest is not affixed, only the class name and output statements are different.

Main program:

usingSystem;usingSystem.ComponentModel.Composition.Hosting;namespacemeftest{classProgram {//a container, used for something. Don't give a damn what you put in.         Private StaticCompositioncontainer container; Static voidMain (string[] args) {            //one of the Assemblycatalog directories that represents a search in an assembly            varAssemblycatalog =NewAssemblycatalog (typeof(program). Assembly);//This sentence is not really useful here, because there are no instances in this assembly that we need (various handler)//Search in a DLL in a directory.             varDirectorycatalog =NewDirectorycatalog (AppDomain.CurrentDomain.BaseDirectory,"*.dll"); varAggregatecatalog =NewAggregatecatalog (Assemblycatalog, Directorycatalog); //create the searched parts and place them in the container. container =NewCompositioncontainer (Aggregatecatalog); varExports = container. getexports<Object> ();//gets all the exported parts (object type).             foreach(varIteminchexports) {Console.WriteLine (item.            Value);        } console.readline (); }    }}

Compile two projects and copy the generated class library file Parts.dll to the Bin\Debug folder of the main program

To run the main program:

As you can see, the class name (object) is printed out. ToString ()). We have successfully created instances of three classes, but the main program does not refer to this class library.

It can also be said that we have successfully injected instances of the class into the main program.

From this small example, we can learn to use MEF three steps: 1, export the desired type (part), 2, find in the appropriate directory (Assemblycatalog, Directorycatalog). 3. Add the found parts to the container.

You can then use the already new instance of the container.

Attention:

The tag above the class Txtfilehandler [Export (typeof (object)]) indicates that this class needs to be exported and that the exported type is Object

In the main function, var exports = container. Getexports<object> ();//Gets all the exported parts (object type).

The type of export and the type to be obtained must be consistent, which is known as a contract.

Most hate the world article a big copy, please do not reprint.

MEF Primer superficial understanding, but try to be simple and clear (a)

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.