Nasıl yapılır: Yazdırma Sistemi Nesnesi Özelliklerini Yansıma Olmadan Alma

Bir nesne üzerindeki özellikleri (ve bu özelliklerin türleri) silmek için yansıma kullanımı, uygulama performansını düşürebilir. System.Printing.IndexedPropertiesAd alanı, bu bilgileri yansıma kullanımı ile elde etmek için bir yol sağlar.

Örnek

Bunu yapmanın adımları aşağıdaki gibidir.

  1. Türün bir örneğini oluşturun. aşağıdaki örnekte, türü PrintQueue Microsoft .NET Framework ile birlikte gelen türdür, ancak neredeyse özdeş kod, içinden türettiğiniz türler için çalışmalıdır PrintSystemObject .

  2. Türünden bir oluşturun PrintPropertyDictionaryPropertiesCollection . ValueBu sözlükteki her girdinin özelliği, öğesinden türetilen türlerden birinin bir nesnesidir PrintProperty .

  3. Sözlüğün üyelerini numaralandırın. Bunların her biri için aşağıdakileri yapın.

  4. Yukarı-her girdinin değerini öğesine atayın PrintProperty ve bir nesnesi oluşturmak için kullanın PrintProperty .

  5. ValueHer nesnenin türünü alır 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()

Ayrıca bkz.