ImportAttribute 类

定义

指定属性、字段或参数值应由 CompositionContainer 对象提供。Specifies that a property, field, or parameter value should be provided by the CompositionContainer.object.

public ref class ImportAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property, AllowMultiple=false, Inherited=false)]
public class ImportAttribute : Attribute
type ImportAttribute = class
    inherit Attribute
Public Class ImportAttribute
Inherits Attribute
继承
ImportAttribute
属性

示例

下面的示例演示三个类,这些类具有用 ImportAttribute 修饰的成员和三个匹配它们的导出。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 String data = "Test Data 1.";
}

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

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 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();
        _container.SatisfyImportsOnce(test1);
        _container.SatisfyImportsOnce(test2);
        _container.SatisfyImportsOnce(test3);
        Console.WriteLine(test1.importedMember.data);
        Console.WriteLine(test2.importedMember.data);
        Console.WriteLine(test3.importedMember.data);
        Console.ReadLine();
    }
}
'Default export infers type and contract name from the
'exported type.  This is the preferred method.
<Export()>
Public Class MyExport1
    Public ReadOnly Property data As String
        Get
            Return "Test Data 1."
        End Get
    End Property
End Class

Public Class MyImporter1

    <Import()>
    Public Property ImportedMember As MyExport1

End Class

Public Interface MyInterface

End Interface

'Specifying the contract type may be important if
'you want to export a type other then the base type,
'such as an interface.
<Export(GetType(MyInterface))>
Public Class MyExport2
    Implements MyInterface
    Public ReadOnly Property data As String
        Get
            Return "Test Data 2."
        End Get
    End Property
End Class

Public Class MyImporter2
    'The import must match the contract type!
    <Import(GetType(MyInterface))>
    Public Property ImportedMember As MyExport2
End Class

'Specifying a contract name should only be 
'needed in rare caes. Usually, using metadata
'is a better approach.
<Export("MyContractName", GetType(MyInterface))>
Public Class MyExport3
    Implements MyInterface
    Public ReadOnly Property data As String
        Get
            Return "Test Data 3."
        End Get
    End Property
End Class

Public Class MyImporter3
    'Both contract name and type must match!
    <Import("MyContractName", GetType(MyInterface))>
    Public Property ImportedMember As MyExport3
End Class



Sub Main()
    Dim catalog As AggregateCatalog = New AggregateCatalog()
    catalog.Catalogs.Add(New AssemblyCatalog(GetType(MyExport1).Assembly))
    Dim container As CompositionContainer = New CompositionContainer(catalog)
    Dim test1 As MyImporter1 = New MyImporter1()
    Dim test2 As MyImporter2 = New MyImporter2()
    Dim test3 As MyImporter3 = New MyImporter3()
    container.SatisfyImportsOnce(test1)
    container.SatisfyImportsOnce(test2)
    container.SatisfyImportsOnce(test3)
    Console.WriteLine(test1.ImportedMember.data)
    Console.WriteLine(test2.ImportedMember.data)
    Console.WriteLine(test3.ImportedMember.data)
    Console.ReadLine()
End Sub

注解

在特性化编程模型中,ImportAttribute 用于声明给定部件的导入或依赖项。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. 在撰写过程中,部件的导入将由该部件所属的 @no__t 0 对象填充,使用提供给该 CompositionContainer 对象的导出。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. 通常,当您在代码中使用 import 特性时,无需指定其中任何一个,它们将自动从修饰成员的类型中推断。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. 有关元数据的详细信息,请参阅 PartMetadataAttributeFor more information about metadata, see PartMetadataAttribute.

构造函数

ImportAttribute()

通过导入具有默认协定名称的导出,初始化 ImportAttribute 类的新实例。Initializes a new instance of the ImportAttribute class, importing the export with the default contract name.

ImportAttribute(String)

通过导入具有指定协定名称的导出,初始化 ImportAttribute 类的新实例。Initializes a new instance of the ImportAttribute class, importing the export with the specified contract name.

ImportAttribute(String, Type)

通过导入具有指定协定名称和类型的导出,初始化 ImportAttribute 类的新实例。Initializes a new instance of the ImportAttribute class, importing the export with the specified contract name and type.

ImportAttribute(Type)

通过导入具有派生自指定类型的协定名称的导出,初始化 ImportAttribute 类的新实例。Initializes a new instance of the ImportAttribute class, importing the export with the contract name derived from the specified type.

属性

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.

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.

ContractName

获取要导入的导出结果的协定名称。Gets the contract name of the export to import.

ContractType

获取要导入的导出结果的类型。Gets the type of the export to import.

RequiredCreationPolicy

获取或设置一个值,该值指示导入程序需要导出结果的特定 CreationPolicy,以满足此导入。Gets or sets a value that indicates that the importer requires a specific CreationPolicy for the exports used to satisfy this import.

Source

获取或设置指定可能满足此导出的域的值。Gets or sets a value that specifies the scopes from which this import may be satisfied.

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(继承自 Attribute)

方法

Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(继承自 Attribute)
GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

(继承自 Attribute)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(继承自 Attribute)
Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(继承自 Attribute)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

显式接口实现

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

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(继承自 Attribute)

适用于

另请参阅