MEF Study Notes (3): Declare export

Source: Internet
Author: User
Export Declaration

Component pass[System. componentmodel. Composition. exportattribute]Export the feature declaration. In MEF, there are several different methods to declare and export, including Part level and pass attributes and methods.

Component Export

When a component needs to be exported, it is often used at the component level. To allow the part to export itself, the simple method is to use[System. componentmodel. Composition. exportattribute]A feature modifies a component, as shown below:

 
[Export]Public ClassSomecomposablepart {...}

Export Properties

The widget can also export properties. Attribute export has the following advantages:

    • They allow export of the seal type, such as the core CLR type or other third-party type.
    • They allow decoupling from how to create and export. For example, export the running environment to your existing httpcontext.
    • They allow members with the same component relationship to be exported. For example, a defaultsendersregistry component exports a sender as an attribute by default.

For example, you have a configuration class to export an integer "timeout" contract, as shown below:CodeSame:

   Public   Class  Configuration {[Export (  "  Timeout  "  )]  Public   Int  Timeout {  Get { Return   Int . Parse (configurationmanager. receivettings [ " Timeout  "  ]) ;}} [Export]  Public   Class  Usestimeout {[import (  "  Timeout  "  )]  Public   Int Timeout { Get ; Set  ;}} 

Method Export

Method export is a part Export method. The method is used as a delegate for export, which is specified in the export contract. Method export has the following benefits:

    • They allow fine-grained controls for export. For example, a rule engine needs to import a plug-in setting method for export.
    • They protect callers from the type string of Task Knowledge.
    • They can be generated through lightweight code intelligence, and you cannot use other export methods.

Note: Due to framework limitations, method export cannot exceed 4 parameters.

In the following example,MessagesenderClass to export itsSendMethodAction <string>Delegate. Processor imports the same delegate.

   Public   Class  Messagesender {[Export (  Typeof (Action < String > )]  Public  Void Send ( String  Message) {console. writeline (Message) ;}} [Export]  Public   Class  Processor {[import (  Typeof (Action < String > )]  Public Action < String > Messagesender { Get ; Set ;}  Public   Void  Send () {messagesender (  "  Processed  "  );}} 

You can also use a simple string contract to export and import data. For example, the "sender" contract used below

   Public   Class  Messagesender {[Export (  "  Messagesender  "  )] Public   Void Send ( String  Message) {console. writeline (Message) ;}} [Export]  Public   Class  Processor {[import (  "  Messagesender  "  )]  Public Action < String > Messagesender { Get ;Set  ;}  Public   Void  Send () {messagesender (  "  Processed  "  );}} 

Note: When exporting using a method, you must provide a type or a string contract name, and cannot leave it blank.

Inherit Export

MEF supports the basic class/basic interface to define the export function, so that it can be inherited. This is the goal of the integrated heritage framework. It uses MEF to discover but does not need to modify existing custom code. To provide this function, useSystem. componentmodel. Composition. inheritedexportattribute. For exampleIloggerThere isInheritedexport. Logger inherits ilogger, so it automatically exports ilogger.

[Inheritedexport]Public InterfaceIlogger {VoidLog (StringMessage );}Public ClassLogger: ilogger {Public VoidLog (StringMessage );}

Discover non-public components

MEF supports the discovery of public and non-public parts. You can achieve this without having to do anything. Note that non-public components in the intermediate/partial trust environment (including Silverlight) are not supported.

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.