Export (0) Print
Expand All

ImportAttribute Class

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

System.Object
  System.Attribute
    System.ComponentModel.Composition.ImportAttribute

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

'Declaration
<AttributeUsageAttribute(AttributeTargets.Property Or AttributeTargets.Field Or AttributeTargets.Parameter, AllowMultiple := False,  _
	Inherited := False)> _
Public Class ImportAttribute _
	Inherits Attribute

The ImportAttribute type exposes the following members.

  NameDescription
Public methodImportAttributeInitializes a new instance of the ImportAttribute class, importing the export with the default contract name.
Public methodImportAttribute(String)Initializes a new instance of the ImportAttribute class, importing the export with the specified contract name.
Public methodImportAttribute(Type)Initializes a new instance of the ImportAttribute class, importing the export with the contract name derived from the specified type.
Public methodImportAttribute(String, Type)Initializes a new instance of the ImportAttribute class, importing the export with the specified contract name and type.
Top

  NameDescription
Public propertyAllowDefaultGets 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.
Public propertyAllowRecompositionGets 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.
Public propertyContractNameGets the contract name of the export to import.
Public propertyContractTypeGets the type of the export to import.
Public propertyRequiredCreationPolicyGets or sets a value that indicates that the importer requires a specific CreationPolicy for the exports used to satisfy this import.
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 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.
    [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:
© 2014 Microsoft