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程序设计指南》"; }
}
}
}