CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Hello MEF

233 Views
Copy Code Show/Hide Line Numbers
using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
 
namespace HelloMEF
{
    public interface IGreetings
    {
        void Hello();
    }
 
    [Export(typeof(IGreetings))]
    public class Greetings : IGreetings
    {
        public void Hello()
        {
            Console.WriteLine("Hello world!");
        }
    }
 
    class HelloMEF : IDisposable
    {
        private readonly CompositionContainer _container;
 
        [Import(typeof(IGreetings))]
        private IGreetings _greetings;
 
        public HelloMEF()
        {
            _container = new CompositionContainer(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            _container.ComposeParts(this);
        }
 
        public void Run()
        {
            _greetings.Hello();
        }
 
        public void Dispose()
        {
            _container.Dispose();
        }
 
        static void Main()
        {
            using (var helloMef = new HelloMEF())
                helloMef.Run();
        }
    }
}
by Andy Sherwood
  April 20, 2010 @ 11:32am
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate