Language: C#
Hello MEF
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(); } } }
Tags:
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

