Language: C#
MEF Factory
using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using System.Reflection; namespace MEFFactory { public interface IMessage { string Text { get; set; } } class Program { static void Main(string[] args) { var p = new Program(); p.Run(); } private void Run() { var messages = new MessageRepository(); CompositionFactory.ComposeObject(messages); foreach(var message in messages.Messages) { Console.WriteLine(message.Text); } Console.ReadKey(); } } class CompositionFactory { public static void ComposeObject<T>(T obj) { var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); var container = new CompositionContainer(catalog); container.ComposeParts(obj); } } class MessageRepository { [ImportMany("Messages", typeof(IMessage))] public IEnumerable<IMessage> Messages { get; set; } } [Export("Messages", typeof(IMessage))] class Message1 : IMessage { public string Text { get; set; } public Message1() { Text = "One"; } } [Export("Messages", typeof(IMessage))] class Message2 : IMessage { public string Text { get; set; } public Message2() { Text = "Two"; } } }
Tags:
Description:
Just a throw together console app that composes a separate class using a factory pattern to handle the MEF composition.
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

