标签:style blog http color io os 使用 ar 2014
//这便是我们的契约 Contract public interface ICalculator { int Add(int a, int b); int Sub(int a, int b); } //此处我们导出了契约,这就是一个部件 [Export(typeof(ICalculator))] public class MyCalculator : ICalculator { public int Add(int a, int b) { Console.WriteLine("Add Invoked"); return a + b; } public int Sub(int a, int b) { Console.WriteLine("Sub Invoked"); return a - b; } } //此处我们导出了契约,这就是一个部件 [Export(typeof(ICalculator))] public class MyCalculator : ICalculator { public int Add(int a, int b) { Console.WriteLine("Add Invoked"); return a + b; } public int Sub(int a, int b) { Console.WriteLine("Sub Invoked"); return a - b; } } class Program { private static CompositionContainer _container; static void Main(string[] args) { //AssemblyCatalog 目录的一种,表示在相应的程序集中搜索 var catalog = new AssemblyCatalog(typeof(Program).Assembly); _container = new CompositionContainer(catalog); MathCompute mathCompute = new MathCompute(); _container.ComposeParts(mathCompute);//组装部件 Console.WriteLine(mathCompute.Add(1, 3)); Console.WriteLine(mathCompute.Sub(3, 2)); Console.ReadLine(); } }
标签:style blog http color io os 使用 ar 2014
原文地址:http://www.cnblogs.com/phenixyu/p/3965230.html