Freigeben über


ImportAttribute Class

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

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

Inheritance Hierarchy

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

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

Syntax

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

The ImportAttribute type exposes the following members.

Constructors

  Name Description
Public method ImportAttribute() Initializes a new instance of the ImportAttribute class, importing the export with the default contract name.
Public method ImportAttribute(String) Initializes a new instance of the ImportAttribute class, importing the export with the specified contract name.
Public method ImportAttribute(Type) Initializes a new instance of the ImportAttribute class, importing the export with the contract name derived from the specified type.
Public method ImportAttribute(String, Type) Initializes a new instance of the ImportAttribute class, importing the export with the specified contract name and type.

Top

Properties

  Name Description
Public property AllowDefault 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.
Public property AllowRecomposition 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.
Public property ContractName Gets the contract name of the export to import.
Public property ContractType Gets the type of the export to import.
Public property RequiredCreationPolicy Gets or sets a value that indicates that the importer requires a specific CreationPolicy for the exports used to satisfy this import.

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

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.