MEF export metadata

Source: Internet
Author: User

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 ;}
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.