聲明導出解釋了部件導出服務的基礎知識和價值觀(Values)。有時候出於種種緣由,導出關聯信息是很是必要的。一般,用於解釋關於功能公共契約的具體實現。容許導入知足約束要求的導出,或者導入全部可用的實現而且在導出前在運行時檢查他們的功能。html
[Export(typeof(IMessageSender))] [ExportMetadata("transport", "smtp")] public class EmailSender : IMessageSender { public void Send(string message) { Console.WriteLine(message); } } [Export(typeof(IMessageSender))] [ExportMetadata("transport", "smtp")] [ExportMetadata("secure", null)] public class SecureEmailSender : IMessageSender { public void Send(string message) { Console.WriteLine(message); } } [Export(typeof(IMessageSender))] [ExportMetadata("transport", "phone_network")] public class SMSSender : IMessageSender { public void Send(string message) { Console.WriteLine(message); } } public interface IMessageSender { void Send(string message); }
[MetadataAttribute] [AttributeUsage(AttributeTargets.Class, AllowMultiple=false)] public class MessageSenderAttribute : ExportAttribute { public MessageSenderAttribute() : base(typeof(IMessageSender)) { } public MessageTransport Transport { get; set; } public bool IsSecure { get; set; } } public enum MessageTransport { Undefined, Smtp, PhoneNetwork, Other } public interface IMessageSender { void Send(string message); }
[MessageSender(Transport=MessageTransport.Smtp)] public class EmailSender : IMessageSender { public void Send(string message) { Console.WriteLine(message); } } [MessageSender(Transport=MessageTransport.Smtp, IsSecure=true)] public class SecureEmailSender : IMessageSender { public void Send(string message) { Console.WriteLine(message); } } [MessageSender(Transport=MessageTransport.PhoneNetwork)] public class SMSSender : IMessageSender { public void Send(string message) { Console.WriteLine(message); } }
public interface IMessageSenderCapabilities { MessageTransport Transport { get; } bool IsSecure { get; } }
public class HttpServerHealthMonitor { [ImportMany] public Lazy<IMessageSender, IMessageSenderCapabilities>[] Senders { get; set; } public void SendNotification() { Compose(); foreach (var sender in Senders) { if (sender.Metadata.Transport == MessageTransport.Smtp && sender.Metadata.IsSecure) { var messageSender = sender.Value; messageSender.Send("Server is fine"); break; } } } private void Compose() { //var container = new CompositionContainer(); //container.ComposeParts(this, new EmailSender()); AssemblyCatalog catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); var container = new CompositionContainer(catalog); container.ComposeParts(this); } }
public class HttpServerHealthMonitor { [ImportMany] public Lazy<IMessageSender, IDictionary<string, object>>[] Senders { get; set; } public void SendNotification() { Compose(); foreach (var sender in Senders) { if (sender.Metadata.ContainsKey("Transport")) { var messageSender = sender.Value; messageSender.Send("Server is fine"); } } } private void Compose() { //var container = new CompositionContainer(); //container.ComposeParts(this, new EmailSender()); AssemblyCatalog catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); var container = new CompositionContainer(catalog); container.ComposeParts(this); } }
public interface IMessageSenderCapabilities { MessageTransport Transport { get; } [DefaultValue(false)] bool IsSecure { get; } }