本文共 7283 字,大约阅读时间需要 24 分钟。
// Author: TerryLee 2008.8.30// URL: [url]http://www.cnblogs.com/Terrylee[/url]static void Main(string[] args){ Console.WriteLine("This is a simple string.");}
// Author: TerryLee 2008.8.30// URL: [url]http://www.cnblogs.com/Terrylee[/url]public class WeekStringProvider{ [Export("Caption")] public String OutputTitle { get { return "星期六"; } }}
// Author: TerryLee 2008.8.30// URL: [url]http://www.cnblogs.com/Terrylee[/url]public class Client{ [Import("Caption")] public String OutputTitle { get; set; }}
// Author: TerryLee 2008.8.30// URL: [url]http://www.cnblogs.com/Terrylee[/url]static void Main(string[] args){ Client client = new Client(); CompositionContainer container = new CompositionContainer(); container.AddComponent(client); container.AddComponent (new WeekStringProvider()); container.Bind(); Console.WriteLine(client.OutputTitle);}
// Author: TerryLee 2008.8.30// URL: [url]http://www.cnblogs.com/Terrylee[/url]public class DateStringProvider{ [Export("Caption")] public String OutputTitle { get { return DateTime.Now.ToLongDateString(); } }}
// Author: TerryLee 2008.8.30// URL: [url]http://www.cnblogs.com/Terrylee[/url]static void Main(string[] args){ Client client = new Client(); CompositionContainer container = new CompositionContainer(); container.AddComponent(client); container.AddComponent (new DateStringProvider()); container.Bind(); Console.WriteLine(client.OutputTitle);}
// Author: TerryLee 2008.8.30// URL: [url]http://www.cnblogs.com/Terrylee[/url]public interface IStringProvider{ String OutputTitle { get; set; }}
// Author: TerryLee 2008.8.30// URL: [url]http://www.cnblogs.com/Terrylee[/url]public class DateStringProvider : IStringProvider { [Export(typeof(IStringProvider))] public String OutputTitle { get { return DateTime.Now.ToLongDateString(); } }}
// Author: TerryLee 2008.8.30// URL: [url]http://www.cnblogs.com/Terrylee[/url]public class Client{ [Import(typeof(IStringProvider))] public String OutputTitle { get; set; }}
// Author: TerryLee 2008.8.30// URL: [url]http://www.cnblogs.com/Terrylee[/url][ContractType("TerryLeeCalculatro")]public interface ICalculator{ int Execute(int x, int y);}public class Client{ [Import(typeof(ICalculator))] public ICalculator Calculator { get; set; }}
// Author: TerryLee 2008.8.30// URL: [url]http://www.cnblogs.com/Terrylee[/url][Export(typeof(ICalculator))]public class SubCalculator : ICalculator{ public int Execute(int x, int y) { return x - y; }}
// Author: TerryLee 2008.8.30// URL: [url]http://www.cnblogs.com/Terrylee[/url]static void Main(string[] args){ Client client = new Client(); CompositionContainer container = new CompositionContainer(); container.AddComponent(client); container.AddComponent (new SubCalculator()); container.Bind(); Console.WriteLine(client.Calculator.Execute(1,2));}
// Author: TerryLee 2008.8.30// URL: [url]http://www.cnblogs.com/Terrylee[/url][ContractType("TerryLeeCalculatro")]public interface IMyCalculator{ int Execute(int x, int y);}[Export(typeof(IMyCalculator))]public class AddCalculator : IMyCalculator{ public int Execute(int x, int y) { return x + y; }}
// Author: TerryLee 2008.8.30// URL: [url]http://www.cnblogs.com/Terrylee[/url]static void Main(string[] args){ Client client = new Client(); CompositionContainer container = new CompositionContainer(); container.AddComponent(client); container.AddComponent (new AddCalculator()); container.Bind(); Console.WriteLine(client.Calculator.Execute(1,2));}
// Author: TerryLee 2008.8.30// URL: [url]http://www.cnblogs.com/Terrylee[/url]public class User{ [Import("Role")] public String Role { get; set; }}
// Author: TerryLee 2008.8.30// URL: [url]http://www.cnblogs.com/Terrylee[/url]public class DatabaseProvider{ [Export("Role")] public String AvailableRole { get { return "Developer"; } }}
// Author: TerryLee 2008.8.30// URL: [url]http://www.cnblogs.com/Terrylee[/url]static void Main(string[] args){ User user = new User(); DirectoryWatchingComponentCatalog catalog = new DirectoryWatchingComponentCatalog(); catalog.AddDirectory(@"Extensions"); CompositionContainer container = new CompositionContainer(catalog.Resolver); container.AddComponent(user); container.Bind(); Console.WriteLine(user.Role); Console.ReadLine();}