Bagikan melalui


Cara: Mendapatkan Properti Objek Sistem Cetak Tanpa Refleksi

Menggunakan refleksi untuk itemisasi properti (dan jenis properti tersebut) pada objek dapat memperlambat performa aplikasi. Namespace System.Printing.IndexedProperties menyediakan sarana untuk mendapatkan informasi ini tanpa menggunakan pantulan.

Contoh

Langkah-langkah untuk melakukan ini adalah sebagai berikut.

  1. Buat sebuah instans dari tipe tersebut. Dalam contoh di bawah ini, tipe tersebut adalah tipe PrintQueue yang disertakan dengan Microsoft .NET Framework, tetapi kode yang hampir identik harus berfungsi untuk tipe yang Anda dapatkan dari PrintSystemObject.

  2. Buat PrintPropertyDictionary dari jenis PropertiesCollection. Properti Value dari setiap entri dalam kamus ini adalah objek dari salah satu jenis yang berasal dari PrintProperty.

  3. Menghitung anggota kamus. Untuk masing-masing, lakukan hal berikut.

  4. Tingkatkan nilai setiap entri ke PrintProperty dan gunakan untuk membuat objek PrintProperty.

  5. Dapatkan jenis dari Value untuk masing-masing objek PrintProperty.


// Enumerate the properties, and their types, of a queue without using Reflection
LocalPrintServer localPrintServer = new LocalPrintServer();
PrintQueue defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue();

PrintPropertyDictionary printQueueProperties = defaultPrintQueue.PropertiesCollection;

Console.WriteLine("These are the properties, and their types, of {0}, a {1}", defaultPrintQueue.Name, defaultPrintQueue.GetType().ToString() +"\n");

foreach (DictionaryEntry entry in printQueueProperties)
{
    PrintProperty property = (PrintProperty)entry.Value;

    if (property.Value != null)
    {
        Console.WriteLine(property.Name + "\t(Type: {0})", property.Value.GetType().ToString());
    }
}
Console.WriteLine("\n\nPress Return to continue...");
Console.ReadLine();


' Enumerate the properties, and their types, of a queue without using Reflection
Dim localPrintServer As New LocalPrintServer()
Dim defaultPrintQueue As PrintQueue = LocalPrintServer.GetDefaultPrintQueue()

Dim printQueueProperties As PrintPropertyDictionary = defaultPrintQueue.PropertiesCollection

Console.WriteLine("These are the properties, and their types, of {0}, a {1}", defaultPrintQueue.Name, defaultPrintQueue.GetType().ToString() + vbLf)

For Each entry As DictionaryEntry In printQueueProperties
    Dim [property] As PrintProperty = CType(entry.Value, PrintProperty)

    If [property].Value IsNot Nothing Then
        Console.WriteLine([property].Name & vbTab & "(Type: {0})", [property].Value.GetType().ToString())
    End If
Next entry
Console.WriteLine(vbLf & vbLf & "Press Return to continue...")
Console.ReadLine()

Lihat juga