Share via


Procedura: ottenere le proprietà di un oggetto di sistema di stampa senza ricorrere alla reflection

L'utilizzo della reflection per descrivere in dettaglio le proprietà (e i relativi tipi) su un oggetto può influire negativamente sulle prestazioni dell'applicazione. Lo spazio dei nomi System.Printing.IndexedProperties consente di ottenere queste informazioni utilizzando la reflection.

Esempio

I passaggi di questa procedura sono indicati di seguito.

  1. Creare un'istanza del tipo. Nell'esempio riportato di seguito, il tipo è il tipo PrintQueue fornito con Microsoft .NET Framework, ma codice quasi identico dovrebbe funzionare per tipi che è possibile derivare da PrintSystemObject.

  2. Creare un oggetto PrintPropertyDictionary dalla proprietà PropertiesCollection del tipo. La proprietà Value di ciascun elemento di questo dizionario è un oggetto di uno dei tipi derivati da PrintProperty.

  3. Enumerare i membri del dizionario. Per ognuno di essi, eseguire le operazioni seguenti.

  4. Eseguire l'upcast del valore di ogni voce in PrintProperty e utilizzarlo per creare un oggetto PrintProperty.

  5. Ottenere il tipo della proprietà Value di ogni oggetto PrintProperty.


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


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

Vedere anche

Riferimenti

PrintProperty

PrintSystemObject

System.Printing.IndexedProperties

PrintPropertyDictionary

LocalPrintServer

PrintQueue

DictionaryEntry

Concetti

Documenti in WPF

Cenni preliminari sulla stampa