ImportAttribute 클래스

정의

CompositionContainer 개체에서 속성, 필드 또는 매개 변수 값을 제공하도록 지정합니다.

public ref class ImportAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property, AllowMultiple=false, Inherited=false)]
public class ImportAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property, AllowMultiple=false, Inherited=false)>]
type ImportAttribute = class
    inherit Attribute
Public Class ImportAttribute
Inherits Attribute
상속
ImportAttribute
특성

예제

다음 예제에서는 멤버가 데코레이트된 세 개의 클래스와 ImportAttribute일치하는 3개의 내보내기를 보여 줍니다.

//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 에서 지정된 부분의 가져오기 또는 종속성을 선언하는 데 사용됩니다. 속성, 필드 또는 메서드를 데코레이트할 수 있습니다. 컴퍼지션 중에 파트의 가져오기는 해당 개체에 CompositionContainer 제공된 CompositionContainer 내보내기를 사용하여 해당 파트가 속한 개체로 채워집니다.

가져오기가 지정된 내보내기와 일치하는지 여부는 주로 계약 이름과 계약 유형을 비교하여 결정됩니다. 일반적으로 코드에서 가져오기 특성을 사용할 때 이러한 특성 중 하나를 지정할 필요가 없으며 데코레이팅된 멤버의 형식에서 자동으로 유추됩니다. 가져오기가 다른 형식의 내보내기(예: 데코레이팅된 멤버 형식의 하위 클래스 또는 해당 멤버가 구현한 인터페이스)의 내보내기와 일치해야 하는 경우 계약 형식을 명시적으로 지정해야 합니다. 예를 들어 동일한 형식의 여러 계약을 구분하기 위해 계약 이름을 명시적으로 지정할 수도 있지만 일반적으로 메타데이터를 통해 이 작업을 수행하는 것이 좋습니다. 메타데이터에 대한 자세한 내용은 을 참조하세요 PartMetadataAttribute.

생성자

ImportAttribute()

기본 계약 이름을 가진 내보내기를 가져와 ImportAttribute 클래스의 새 인스턴스를 초기화합니다.

ImportAttribute(String)

지정된 계약 이름을 가진 내보내기를 가져와 ImportAttribute 클래스의 새 인스턴스를 초기화합니다.

ImportAttribute(String, Type)

지정된 계약 이름과 형식을 가진 내보내기를 가져와 ImportAttribute 클래스의 새 인스턴스를 초기화합니다.

ImportAttribute(Type)

지정된 형식에서 파생된 계약 이름을 가진 내보내기를 가져와 ImportAttribute 클래스의 새 인스턴스를 초기화합니다.

속성

AllowDefault

컨테이너에 해당 계약 이름을 가진 내보내기가 없는 경우 속성, 필드 또는 매개 변수가 해당 형식의 기본값으로 설정되는지 여부를 나타내는 값을 가져오거나 설정합니다.

AllowRecomposition

컨테이너에서 일치하는 계약이 있는 내보내기가 변경된 경우 속성이나 필드를 재작성해야 하는지 여부를 나타내는 값을 가져오거나 설정합니다.

ContractName

가져올 내보내기의 계약 이름을 가져옵니다.

ContractType

가져올 내보내기의 형식을 가져옵니다.

RequiredCreationPolicy

이 가져오기를 충족시키는 데 사용되는 내보내기에 대한 특정 CreationPolicy가 필요함을 나타내는 값을 가져오거나 설정합니다.

Source

이 가져오기를 충족할 수 있는 범위를 지정하는 값을 가져오거나 설정합니다.

TypeId

파생 클래스에서 구현된 경우 이 Attribute에 대한 고유 식별자를 가져옵니다.

(다음에서 상속됨 Attribute)

메서드

Equals(Object)

이 인스턴스가 지정된 개체와 같은지를 나타내는 값을 반환합니다.

(다음에서 상속됨 Attribute)
GetHashCode()

이 인스턴스의 해시 코드를 반환합니다.

(다음에서 상속됨 Attribute)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
IsDefaultAttribute()

파생 클래스에서 재정의된 경우 이 인스턴스 값이 파생 클래스에 대한 기본값인지 여부를 표시합니다.

(다음에서 상속됨 Attribute)
Match(Object)

파생 클래스에서 재정의된 경우 이 인스턴스가 지정된 개체와 같은지 여부를 나타내는 값을 반환합니다.

(다음에서 상속됨 Attribute)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

명시적 인터페이스 구현

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

이름 집합을 해당하는 디스패치 식별자 집합에 매핑합니다.

(다음에서 상속됨 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

인터페이스의 형식 정보를 가져오는 데 사용할 수 있는 개체의 형식 정보를 검색합니다.

(다음에서 상속됨 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

개체에서 제공하는 형식 정보 인터페이스의 수를 검색합니다(0 또는 1).

(다음에서 상속됨 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

개체에서 노출하는 메서드와 속성에 대한 액세스를 제공합니다.

(다음에서 상속됨 Attribute)

적용 대상

추가 정보