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

Source: Internet
Author: User
Tags processing text

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

In the previous article, we have learned basic MEF concepts and usage methods.

However, we export an object-type instance, which can only be used for tostring. It does not reference the part class library or use the member methods in it.

In this article, we gradually move closer to the goal of a simple File Manager.

Create a class library IPart and add an interface IFileHandler. cs

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

The Parts class library and the main project meftest both reference IPart.

In the Parts class library, change the type to be exported to IFileHandler, and implement the IFileHandler class.

Take one of them as an example.

Using IPart; using System. componentModel. composition; namespace Parts {[Export (typeof (IFileHandler)] // indicates that this class needs to be exported. The Export type is IFileHandler public class TxtFileHandler: IFileHandler {public void Process () {Console. writeLine ("processing text files ");}}}

Main project:

Using IPart; using System. ComponentModel. Composition. Hosting; namespace meftest {class Program {// container for installation. No matter what you install. Private static CompositionContainer container; static void Main (string [] args) {// AssemblyCatalog Directory, which indicates searching 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> (); // obtain all exported parts (IFileHandler type ). Foreach (var item in exports) {item. Value. Process (); // IFileHandler. Process () can be called here} Console. ReadLine ();}}}

After compilation, do not forget to copy Parts. dll to the main program's bin \ debug.

Run:

We have created all the IHandler instances and run the member functions.

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.