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

Top

Remarks

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.

Examples

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