using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel.Composition.Hosting; using System.ComponentModel.Composition; using System.Reflection; namespace MessageConsoleApplication { public class Program { [Import] public IMessageSender MessageSender { get; set; } public static void Main(string[] args) { Program p = new Program(); p.Run(); } public void Run() { Compose(); MessageSender.Send("Message Sent"); } private void Compose() { AssemblyCatalog catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); var container = new CompositionContainer(catalog); container.ComposeParts(this); } } public interface IMessageSender { void Send(string message); } [Export(typeof(IMessageSender))] public class EmailSender : IMessageSender { public void Send(string message) { Console.WriteLine(message); } } } 一个完整的,可以用VS2010测试用的例子。.NET Framework 4.0.