Export metadata
When exporting parts, you need to export the associated data,
You can use exportmetadata to export metadata.
Simple metadata export instance, see http://mef.codeplex.com/
Using system;
Using system. componentmodel. composition;
Using system. componentmodel. Composition. Hosting;
Namespace metadata
{
Class Program
{
[Import]
Public httpserverhealthmoniter HHM {Get; set ;}
Static void main (string [] ARGs)
{
Program P = new program ();
P. Run ();
}
Public void run ()
{
Compent ();
HHM. sendernotification ();
Console. Read ();
}
/// <Summary>
/// Combined container
/// </Summary>
Private void compent ()
{
VaR catalog = new assemblycatalog (system. reflection. Assembly. getexecutingassembly ());
VaR Container = new compositioncontainer (Catalog );
Container. composeparts (this );
}
}
Public interface imessagesender
{
Void send (string message );
}
# Region
/*
[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 );
}
}
*/
# Endregion
[Metadataattribute]
[Attributeusage (attributetargets. Class, allowmultiple = false)]
Public class messagesenderattribute: exportattribute
{
Public messagesenderattribute (): Base (typeof (imessagesender )){}
Public messagetransport transport {set; get ;}
Public bool issecure {Get; set ;}
}
Public Enum messagetransport
{
Undifined,
SMTP,
Phonenetwork,
Other
}
[Export (typeof (imessagesender)]
[Messagesender (Transport = messagetransport. SMTP)]
Public class emailsender: imessagesender
{
Public void send (string message)
{
Console. writeline (Message );
}
}
[Export (typeof (imessagesender)]
[Messagesender (Transport = messagetransport. SMTP, issecure = true)]
Public class secureemailsender: imessagesender
{
Public void send (string message)
{
Console. writeline (Message );
}
}
[Export (typeof (imessagesender)]
[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 ;}
}
[Export]
Public class httpserverhealthmoniter
{
[Importtasks]
Public lazy <imessagesender, imessagesendercapabilities> [] senders {Get; set ;}
Public void sendernotification ()
{
Foreach (VAR sender in senders)
{
If (sender. Metadata. Transport = messagetransport. SMTP & sender. Metadata. issecure)
{
VaR messagersender = sender. value;
Messagersender. Send ("server is fine! ");
Break;
}
}
}
}
}
[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 );
}
}
Metadata filtering and default values
Public interface imessagesendercapabilities
{
Messagetransport transport {Get ;}
[Defaultvalue (false)];
Bool issecure {Get ;}
}