namespace MEFDemo
{ class Program { private CompositionContainer _container; [ImportMany(typeof(ICalculator))] IEnumerable<Lazy<ICalculator>> calculators; public Program() { var catalog = new AssemblyCatalog(typeof(Program).Assembly); _container = new CompositionContainer(catalog); try { this._container.ComposeParts(this); } catch (CompositionException compositionException) { Console.WriteLine(compositionException.ToString()); } } static void Main(string[] args) { Program p = new Program(); string s; Console.WriteLine("Enter Command:"); while (true) { s = Console.ReadLine(); foreach (Lazy<ICalculator> i in p.calculators) { Console.WriteLine(i.Value.Calculate(s)); } } } } public interface ICalculator { String Calculate(String input); } [Export(typeof(ICalculator))] class Add : ICalculator { string ICalculator.Calculate(string input) { return "Symbol=a,输入值" + input; } } [Export(typeof(ICalculator))] class Add2 : ICalculator { string ICalculator.Calculate(string input) { return "Symbol=b,输入值" + input; } }}