ExportAttribute Class

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

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)

[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Field, AllowMultiple = true, 
	Inherited = false)]
public class ExportAttribute : Attribute

The ExportAttribute type exposes the following members.

  NameDescription
Public methodExportAttribute()Initializes a new instance of the ExportAttribute class, exporting the type or member marked with this attribute under the default contract name.
Public methodExportAttribute(String)Initializes a new instance of the ExportAttribute class, exporting the type or member marked with this attribute under the specified contract name.
Public methodExportAttribute(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 methodExportAttribute(String, Type)Initializes a new instance of the ExportAttribute class, exporting the specified type under the specified contract name.
Top

  NameDescription
Public propertyContractNameGets the contract name that is used to export the type or member marked with this attribute.
Public propertyContractTypeGets the contract type that is exported by the member that this attribute is attached to.
Top

  NameDescription
Public methodEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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.

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; }
    }

Silverlight

Supported in: 5, 4

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

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

Community Additions

ADD
Show: