Udostępnij za pośrednictwem


ExportAttribute Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Specifies that a type, property, field, or method provides a particular export.

Inheritance Hierarchy

System.Object
  System.Attribute
    System.ComponentModel.Composition.ExportAttribute
      System.ComponentModel.Composition.InheritedExportAttribute

Namespace:  System.ComponentModel.Composition
Assembly:  System.ComponentModel.Composition (in System.ComponentModel.Composition.dll)

Syntax

'Declaration
<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Method Or AttributeTargets.Property Or AttributeTargets.Field, AllowMultiple := True,  _
    Inherited := False)> _
Public Class ExportAttribute _
    Inherits Attribute
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Field, AllowMultiple = true, 
    Inherited = false)]
public class ExportAttribute : Attribute

The ExportAttribute type exposes the following members.

Constructors

  Name Description
Public method ExportAttribute() Initializes a new instance of the ExportAttribute class, exporting the type or member marked with this attribute under the default contract name.
Public method ExportAttribute(String) Initializes a new instance of the ExportAttribute class, exporting the type or member marked with this attribute under the specified contract name.
Public method ExportAttribute(Type) Initializes a new instance of the ExportAttribute class, exporting the type or member marked with this attribute under a contract name derived from the specified type.
Public method ExportAttribute(String, Type) Initializes a new instance of the ExportAttribute class, exporting the specified type under the specified contract name.

Top

Properties

  Name Description
Public property ContractName Gets the contract name that is used to export the type or member marked with this attribute.
Public property ContractType Gets the contract type that is exported by the member that this attribute is attached to.

Top

Methods

  Name Description
Public method Equals Infrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Returns the hash code for this instance. (Inherited from Attribute.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Match When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

In the Attributed Programming Model, the ExportAttribute declares that a part exports, or provides to the composition container, an object that fulfills a particular contract. During composition, parts with imports that have matching contracts will have those dependencies filled by the exported object.

The ExportAttribute can decorate either an entire class, or a property, field, or method of a class. If the entire class is decorated, an instance of the class is the exported object. If a member of a class is decorated, the exported object will be the value of the decorated member.

Whether or not a contract matches is determined primarily by the contract name and the contract type. For more information, see ImportAttribute.

Examples

The following example shows three classes decorated with the ExportAttribute, and three imports that match them.

    //Default export infers type and contract name from the
    //exported type.  This is the preferred method.
    [Export]
    public class MyExport1
    {

    }

    public class MyImporter1
    {
        [Import]
        public MyExport1 importedMember { get; set; }
    }

    public interface MyInterface
    {

    }

    //Specifying the contract type may be important if
    //you want to export a type other then the base type,
    //such as an interface.
    [Export(typeof(MyInterface))]
    public class MyExport2
    {

    }

    public class MyImporter2
    {
        //The import must match the contract type!
        [Import(typeof(MyInterface))]
        public MyExport2 importedMember { get; set; }
    }

    //Specifying a contract name should only be 
    //needed in rare caes. Usually, using metadata
    //is a better approach.
    [Export("MyContractName", typeof(MyInterface))]
    public class MyExport3 : MyInterface
    {

    }

    public class MyImporter3
    {
        //Both contract name and type must match!
        [Import("MyContractName", typeof(MyInterface))]
        public MyExport3 importedMember { get; set; }
    }

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.