MEF is easy to get started with, but it strives to be easy to understand (4 ).

Source: Internet
Author: User
Tags processing text

MEF is easy to get started with, but it strives to be easy to understand (4 ).

In the previous article, we can obtain various FileHandler instances and corresponding metadata. In this article, we create a slightly complete file manager.

1. Modify the IFileHandler interface and input the file name.

namespace IPart{    public interface IFileHandler    {        void Process(string fileName);    }}

2. Modify the FileHandler.

Using IPart; using System. componentModel. composition; namespace Parts {[Export (typeof (IFileHandler)] // indicates that this class needs to be exported. The Export type is IFileHandler [ExportMetadata ("Extension ",". txt ")] // export metadata extensionwith the value .txt public class TxtFileHandler: IFileHandler {public void Process (string fileName) {Console. writeLine ("processing text files" + fileName );}}}

3. Modify the main function

Using IPart; using System. ComponentModel. Composition. Hosting; using System. Linq; namespace meftest {class Program {// container for installation. No matter what you install. Private static CompositionContainer container; static void Main (string [] args) {// simulate data. String [] files = new string [] {@ "c: \ xxoo \ xxoo.txt", @ "c: \ xxoo \ ooxx.doc", @ "d: \ test directory \ mm.jpg ", @" e: \ movie \ Tian haiyi. avi ",}; // One Of The AssemblyCatalog directories, indicating searching for var assemblyCatalog = new AssemblyCatalog (typeof (Program) in the Assembly ). assembly); // This sentence is actually useless, because this Assembly does not have any instances (various handler) We need. // you can search for the dll in a directory. Var directoryCatalog = new DirectoryCatalog (AppDomain. currentDomain. baseDirectory ,"*. dll "); var aggregateCatalog = new AggregateCatalog (assemblyCatalog, directoryCatalog); // create a component and place it in the container. Container = new CompositionContainer (aggregateCatalog); var exports = container. getExports <IFileHandler, IPatMetadata> (); // obtain all exported parts (IFileHandler with IPatMetadata type metadata and the metadata name is Extension ). Foreach (var file in files) {string ext = System. IO. path. getExtension (file ). toLower (); var export = exports. singleOrDefault (o => o. metadata. extension = ext); // locate the corresponding processing instance based on the Extension, that is, metadata. If you find multiple instances, a thow error will occur. If (export! = Null) export. Value. Process (file) ;}console. ReadLine ();}}}

Running result:

You can see that the correct processing instance is found for each specific file. Avi file. If the processed instance is not found, the file is not processed.

Extension:

Now we need to be able to process avi, which is very simple. Just copy a Handler and implement the processing logic of the Avi file. Of course, you still need to copy the dll.

Using IPart; using System. componentModel. composition; namespace Parts {[Export (typeof (IFileHandler)] // indicates that this class needs to be exported. The Export type is IFileHandler [ExportMetadata ("Extension ",". avi ")] // export metadata extensionwith the value .txt public class AviFileHandler: IFileHandler {public void Process (string fileName) {Console. writeLine ("play av file" + fileName );}}}

You see, expansion is not very simple. You only need to implement the processing logic, and the main program can process one more file type. The interface and main program do not need to be modified at all.

Compared with other IOC frameworks, MEF does not need configuration files and uses the attribute Method for configuration, which is clear and concise.

Summary:

It takes you ten minutes to complete the series. It takes an hour to complete all the projects. Now you get a new skill, MEF, and reach my level, haha.

I am very fond of learning MEF. The articles in the garden seem to be a little difficult for me. I took some detours and finally understood some of them. Therefore, I want to write a simple and clear introductory tutorial, and I don't know if the goal is achieved.

Tell me, MEF, you are not getting started.

 

I hate a lot of articles in the world. Please do not repost them.

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.