ImportAttribute Kelas

Definisi

Menentukan bahwa nilai properti, bidang, atau parameter harus disediakan oleh 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
[<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
Warisan
ImportAttribute
Atribut

Contoh

Contoh berikut menunjukkan tiga kelas dengan anggota yang dihiasi dengan , dan tiga ekspor yang cocok dengannya ImportAttribute.

//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

Keterangan

Dalam Model Pemrograman Atribut, ImportAttribute digunakan untuk mendeklarasikan impor, atau dependensi, dari bagian tertentu. Ini dapat menghias properti, bidang, atau metode. Selama komposisi, impor bagian akan diisi oleh objek tempat bagian tersebut CompositionContainer berada, dengan menggunakan ekspor yang CompositionContainer disediakan untuk objek tersebut.

Apakah impor cocok dengan ekspor tertentu ditentukan terutama dengan membandingkan nama kontrak dan jenis kontrak. Biasanya, Anda tidak perlu menentukan salah satu dari ini saat menggunakan atribut impor dalam kode, dan atribut tersebut akan secara otomatis disimpulkan dari jenis anggota yang didekorasi. Jika impor harus cocok dengan ekspor dari jenis yang berbeda (misalnya, subkelas dari jenis anggota yang didekorasi, atau antarmuka yang diterapkan oleh anggota tersebut), maka jenis kontrak harus ditentukan secara eksplisit. Nama kontrak juga dapat ditentukan secara eksplisit, misalnya untuk membedakan antara beberapa kontrak dengan jenis yang sama, tetapi biasanya lebih baik untuk melakukan ini melalui metadata. Untuk informasi selengkapnya tentang metadata, lihat PartMetadataAttribute.

Konstruktor

ImportAttribute()

Menginisialisasi instans ImportAttribute baru kelas, mengimpor ekspor dengan nama kontrak default.

ImportAttribute(String)

Menginisialisasi instans ImportAttribute baru kelas, mengimpor ekspor dengan nama kontrak yang ditentukan.

ImportAttribute(String, Type)

Menginisialisasi instans ImportAttribute baru kelas, mengimpor ekspor dengan nama dan jenis kontrak yang ditentukan.

ImportAttribute(Type)

Menginisialisasi instans ImportAttribute baru kelas, mengimpor ekspor dengan nama kontrak yang berasal dari jenis yang ditentukan.

Properti

AllowDefault

Mendapatkan atau menetapkan nilai yang menunjukkan apakah properti, bidang, atau parameter akan diatur ke nilai default jenisnya saat ekspor dengan nama kontrak tidak ada dalam kontainer.

AllowRecomposition

Mendapatkan atau menetapkan nilai yang menunjukkan apakah properti atau bidang akan dikompresi ulang saat ekspor dengan kontrak yang cocok telah berubah dalam kontainer.

ContractName

Mendapatkan nama kontrak ekspor yang akan diimpor.

ContractType

Mendapatkan jenis ekspor yang akan diimpor.

RequiredCreationPolicy

Mendapatkan atau menetapkan nilai yang menunjukkan bahwa importir memerlukan khusus CreationPolicy untuk ekspor yang digunakan untuk memenuhi impor ini.

Source

Mendapatkan atau menetapkan nilai yang menentukan cakupan tempat impor ini mungkin terpenuhi.

TypeId

Ketika diimplementasikan di kelas turunan, mendapatkan pengidentifikasi unik untuk ini Attribute.

(Diperoleh dari Attribute)

Metode

Equals(Object)

Mengembalikan nilai yang menunjukkan apakah instans ini sama dengan objek tertentu.

(Diperoleh dari Attribute)
GetHashCode()

Mengembalikan kode hash untuk instans ini.

(Diperoleh dari Attribute)
GetType()

Mendapatkan dari instans Type saat ini.

(Diperoleh dari Object)
IsDefaultAttribute()

Ketika ditimpa di kelas turunan, menunjukkan apakah nilai instans ini adalah nilai default untuk kelas turunan.

(Diperoleh dari Attribute)
Match(Object)

Saat ditimpa di kelas turunan, mengembalikan nilai yang menunjukkan apakah instans ini sama dengan objek tertentu.

(Diperoleh dari Attribute)
MemberwiseClone()

Membuat salinan dangkal dari saat ini Object.

(Diperoleh dari Object)
ToString()

Mengembalikan string yang mewakili objek saat ini.

(Diperoleh dari Object)

Implementasi Antarmuka Eksplisit

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

Memetakan sekumpulan nama ke sekumpulan pengidentifikasi pengiriman yang sesuai.

(Diperoleh dari Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Mengambil informasi jenis untuk objek, yang dapat digunakan untuk mendapatkan informasi jenis untuk antarmuka.

(Diperoleh dari Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Mengambil jumlah antarmuka informasi jenis yang disediakan objek (baik 0 atau 1).

(Diperoleh dari Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Menyediakan akses ke properti dan metode yang diekspos oleh objek.

(Diperoleh dari Attribute)

Berlaku untuk

Lihat juga