MEF Programming Guide IV: Using MEF Declaration Export (exports) and imports (imports)

Source: Internet
Author: User

In MEF, you use [System.ComponentModel.Composition.ExportAttribute] to support multiple levels of export part configurations, including classes, fields, properties, and Method-level export parts. By looking at Exportattribute's source code, you know that Exportattribute is defined as Attribute and that it is set up for use.

[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
{
   //......
}

You can use [Exportattribute] to annotate any class object or its internal fields, properties, and methods as a component that can be exported as an exportable part. For example, you need to export an object as a component (class-level export), just add [Exportattribute] to the class, and refer to the MEF components (composable Parts) and the contract (contracts) The basic application, the following is the demo code:

[System.ComponentModel.Composition.Export]
public class DBLogger
{
}

For fields, attribute-level exports of the same kind, the sample is annotated with [Exportattribute], and the following code block shows an example of a complete property import and export.

namespace MEFTraining.ExmprtImport
{
     public partial class MainPage : UserControl
     {
         [Import("Name")]
         public string BookName { get; set; }
         public MainPage()
         {
             InitializeComponent();
             CompositionInitializer.SatisfyImports(this);
             MessageBox.Show(BookName);
         }
     }
     public class BookService
     {
         [Export("Name")]
         public string BookName
         {
             get { return "《MEF程序设计指南》"; }
         }
     }
}

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.