Freigeben über


So geht's: Drucksystem-Objekteigenschaften ohne Reflektion abrufen

Die Verwendung von Reflexion zum Auflisten der Eigenschaften (und der Typen dieser Eigenschaften) eines Objekts kann die Leistung der Anwendung beeinträchtigen. Der System.Printing.IndexedProperties-Namespace bietet eine Möglichkeit, diese Informationen ohne Spiegelung zu erhalten.

Beispiel

Die schritte hierfür sind wie folgt.

  1. Erstellen Sie eine Instanz des Typs. Im folgenden Beispiel ist der Typ der PrintQueue-Typ, der mit Microsoft .NET Framework ausgeliefert wird, aber fast identischer Code sollte für Typen funktionieren, die Ihr Gerät von PrintSystemObject ableitet.

  2. Erstellen Sie eine PrintPropertyDictionary aus dem PropertiesCollection des Typs. Die Value-Eigenschaft jedes Eintrags in diesem Wörterbuch ist ein Objekt eines der Typen, die von PrintProperty abgeleitet werden.

  3. Zähle die Elemente des Wörterbuchs auf. Führen Sie für jeden von ihnen die folgenden Schritte aus:

  4. Hochcasten Sie den Wert jedes Eintrags auf PrintProperty und verwenden Sie ihn, um ein PrintProperty-Objekt zu erstellen.

  5. Rufen Sie den Typ von Value jedes einzelnen PrintProperty-Objekts ab.


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

Siehe auch