<file_sep>﻿using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;

namespace IntroToMEF
{
    /// <summary>
    /// Tutorial    : Developing Modular Applications with MEF
    /// URL         : https://blogs.msmvps.com/bsonnino/2013/08/31/developing-modular-applications-with-mef/
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            // Now we need to tell that our parts are not only in the current assembly, 
            // but they can also be found in the current folder.For that we must compose 
            // two catalogs: one for the current assembly(for the imported parts) and 
            // another for the folder (for the exported parts).We will use an AggregateCatalog 
            // to compose both catalogs
            var catalog = new AggregateCatalog(
                new AssemblyCatalog(Assembly.GetExecutingAssembly()),
                new DirectoryCatalog("."));

            // created the container that composes the parts after the menu is created
            var container = new CompositionContainer(catalog);
            var menu = new Menu();
            container.ComposeParts(menu);

            // When the menu.OptionList() method is called, the module’s title is listed
            menu.OptionList();
            Console.ReadLine();
        }
    }
}
<file_sep>﻿using IntroToMEF.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;

namespace IntroToMEF
{
    /// <summary>
    /// In order to match the parts, we must say 
    /// that the imported part is also of the IModule type
    /// </summary>
    public class Menu
    {
        [ImportMany]
        private IEnumerable<IModule> _modules;

        /// <summary>
        /// List all the modules.
        /// </summary>
        public void OptionList()
        {
            // iterate over all the modules that we find
            foreach (var module in _modules)
            {
                // write to console 
                Console.WriteLine(module.Title);
            }
            
        }
    }
}
<file_sep>﻿using IntroToMEF.Interfaces;
using System.ComponentModel.Composition;

namespace IntroToMEF.Modules
{
    /// <summary>
    /// Our class will implement this interface (IModule) and the export 
    /// will tell that we are exporting the IModule interface
    /// </summary>
    [Export(typeof(IModule))]
    public class Customer : IModule
    {
        public Customer()
        {
            Title = "Customers";
        }

        public string Title { get; set; }
    }
}
<file_sep>﻿using IntroToMEF.Interfaces;
using System.ComponentModel.Composition;

namespace IntroToMEF.Modules
{
    /// <summary>
    /// Our class will implement this interface (IModule) and the export 
    /// will tell that we are exporting the IModule interface
    /// </summary>
    [Export(typeof(IModule))]
    public class Product : IModule
    {
        public Product()
        {
            Title = "Products";
        }

        public string Title { get; set; }
    }
}
