MEF programming guide 4: Using MEF to Declare export (exports) and import (imports)

Source: Internet
Author: User
In MEF, [system. componentmodel. Composition. exportattribute] supports multiple levels of export part configurations, including class, field, attribute, and method-level export parts. Source code It is known that exportattribute is defined as attribute and the use range is set for it.

[System. Diagnostics. codeanalysis. suppressmessage ( " Microsoft. Performance " , " Ca1813: avoidunsealedattributes " )]
[Attributeusage (attributetargets. Class | Attributetargets. Field | Attributetargets. Property | Attributetargets. method,
Allowmultiple =   True , Inherited =   False )]
Public   Class Exportattribute: attribute
{
// ......
}

 

When any class object or its internal fields, attributes, and methods need to be composite parts, you can use [exportattribute] to mark them as exported parts. For example, to export an object as a component that can be combined (that is, export at the class level), you only need to add [exportattribute] to the class, for detailed application, see the basic application of composable parts and contracts in MEF.Code:

[System. componentmodel. Composition. Export]
Public   Class Dblogger
{
}

 

Fields and attributes are exported at the same level. [exportattribute] is used for annotation. The following code block demonstrates a complete example of attribute import and export.

Namespace Meftraining. exmprtimport
{
Public   Partial   Class Mainpage: usercontrol
{
[Import ( " Name " )]
Public   String Bookname { Get ; Set ;}

PublicMainpage ()
{
Initializecomponent ();

Compositioninitializer. satisfyimports (This);

MessageBox. Show (bookname );
}
}

Public   Class Bookservice
{
[Export ( " Name " )]
Public   String Bookname
{
Get { Return   " MEFProgramDesign Guide " ;}
}
}
}

 

Method-level import and export are mainly implemented by delegation. The use of both action and action is also very simple, whether it is a parameter or return value required by the method, can be achieved through anonymous delegation. The following code defines a bookservice class, which uses MEF to export the printbookname method with a string type parameter. In this case, you can use anonymous delegation to export the form parameter and method.

Public   Class Bookservice
{
[Export ( Typeof (Action < String > )]
Public   Void Printbookname ( String Name)
{
Console. writeline (name );
}
}

 

To use this method, you only need to use the anonymous delegate method to import the method. The following code is a call example of the preceding export method.

Public   Partial   Class Methodexportimport: usercontrol
{
[Import ( Typeof (Action < String > )]
Public Action < String > Printbookname { Get ; Set ;}

PublicMethodexportimport ()
{
Initializecomponent ();

Compositioninitializer. satisfyimports (This);

Printbookname ("MEF programming guide");
}
}

 

In addition, MEF supports inherited import and export applications, using [system. componentmodel. composition. inheritedexportattribute] implements inheritance-based export. The others are exactly the same as those of fields, attributes, and methods. The following Code demonstrates the inheritance-based export and export applications.

Namespace Meftraining. exmprtimport
{
Public   Partial   Class Inheritedexportimport: usercontrol
{
[Import]
Public Iuserservie uservice { Get ; Set ;}

PublicInheritedexportimport ()
{
Initializecomponent ();
Compositioninitializer. satisfyimports (This);

StringName=Uservice. GetUserName ();
}
}

[Inheritedexport]
Public   Interface Iuserservie
{
String GetUserName ();
}
Public   Class Userservice: iuserservie
{
Public   String GetUserName ()
{
Return   " Zhang San " ;
}
}
}

 

MEF also supports importing constructor parameters. We will not describe them here. If you are interested, you can directly query the MEF English Version Programming Guide.

 

Note: For more information, see:Declaring exports,Declaring imports

MEF Official Website: http://mef.codeplex.com/

Recommendation guide: MEF programming guide 1: Host MEF in an application

MEF Programming Guide 2: Using compositioninitializer to host MEF in Silverlight

MEF programming guide 3: Basic Application of composable parts and contracts in MEF

 

Description

Study NotesArticleWe are willing to learn and exchange with those who are interested in the future. You are welcome to repost this article, but mark the original article connection in a prominent position.

Author: beniao

Article Source: http://beniao.cnblogs.com/or http://www.cnblogs.com/

 

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.