ImportAttribute Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Specifies that a property, field, or parameter value should be provided by the CompositionContainer.object

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


[AttributeUsageAttribute(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, 
	AllowMultiple = false, Inherited = false)]
public class ImportAttribute : Attribute


Initializes a new instance of the ImportAttribute class, importing the export with the default contract name.


Initializes a new instance of the ImportAttribute class, importing the export with the specified contract name.

System_CAPS_pubmethodImportAttribute(String, Type)

Initializes a new instance of the ImportAttribute class, importing the export with the specified contract name and type.


Initializes a new instance of the ImportAttribute class, importing the export with the contract name derived from the specified type.


Gets or sets a value that indicates whether the property, field, or parameter will be set to its type's default value when an export with the contract name is not present in the container.


Gets or sets a value that indicates whether the property or field will be recomposed when exports with a matching contract have changed in the container.


Gets the contract name of the export to import.


Gets the type of the export to import.


Gets or sets a value that indicates that the importer requires a specific CreationPolicy for the exports used to satisfy this import.


Gets or sets a value that specifies the scopes from which this import may be satisfied.


When implemented in a derived class, gets a unique identifier for this Attribute.(Inherited from Attribute.)


This API supports the product infrastructure and is not intended to be used directly from your code. Returns a value that indicates whether this instance is equal to a specified object.(Inherited from Attribute.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Returns the hash code for this instance.(Inherited from Attribute.)


Gets the Type of the current instance.(Inherited from Object.)


When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.(Inherited from Attribute.)


When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.(Inherited from Attribute.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns a string that represents the current object.(Inherited from Object.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Maps a set of names to a corresponding set of dispatch identifiers.(Inherited from Attribute.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Retrieves the type information for an object, which can be used to get the type information for an interface.(Inherited from Attribute.)


Retrieves the number of type information interfaces that an object provides (either 0 or 1).(Inherited from Attribute.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Provides access to properties and methods exposed by an object.(Inherited from Attribute.)

In the Attributed Programming Model, the ImportAttribute is used to declare the imports, or dependencies, of a given part. It can decorate a property, a field, or a method. During composition, a part's imports will be filled by the CompositionContainer object to which that part belongs, by using the exports provided to that CompositionContainer object.

Whether an import matches a given export is determined primarily by comparing the contract name and the contract type. Ordinarily, you do not have to specify either of these when using the import attribute in code, and they will be automatically inferred from the type of the decorated member. If the import must match an export of a different type (for example, a subclass of the type of the decorated member, or an interface implemented by that member), then the contract type must be explicitly specified. The contract name can also be explicitly specified, for example to distinguish between multiple contracts with the same type, but it is usually better to do this through metadata. For more information about metadata, see PartMetadataAttribute.

The following example shows three classes with members decorated with the ImportAttribute, and three exports that match them.

//Default export infers type and contract name from the
//exported type.  This is the preferred method.
public class MyExport1
    public String data = "Test Data 1.";

public class MyImporter1
    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.
public class MyExport2 : MyInterface
    public String data = "Test Data 2.";

public class MyImporter2
    //The import must match the contract type!
    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 String data = "Test Data 3.";

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

class Program

    static void Main(string[] args)
        AggregateCatalog catalog = new AggregateCatalog();
        catalog.Catalogs.Add(new AssemblyCatalog(typeof(MyExport1).Assembly));
        CompositionContainer _container = new CompositionContainer(catalog);
        MyImporter1 test1 = new MyImporter1();
        MyImporter2 test2 = new MyImporter2();
        MyImporter3 test3 = new MyImporter3();


.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Available since 4.0

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

Return to top