Compartir a través de


Cómo: Obtener propiedades de un objeto de sistema de impresión sin reflexión

Utilizar la reflexión para detallar las propiedades (y los tipos de esas propiedades) de un objeto puede deteriorar el rendimiento de la aplicación. El espacio de nombres System.Printing.IndexedProperties proporciona un medio para obtener esta información sin utilizar la reflexión.

Ejemplo

Los pasos para hacerlo son los siguientes.

  1. Cree una instancia del tipo. En el ejemplo siguiente, el tipo es el tipo PrintQueue que se distribuye con Microsoft .NET Framework, pero un código prácticamente idéntico debería funcionar para los tipos que derive de PrintSystemObject.

  2. Cree PrintPropertyDictionary a partir de la propiedad PropertiesCollection del tipo. La propiedad Value de cada entrada de este diccionario es un objeto de uno de los tipos derivados de PrintProperty.

  3. Enumere los miembros del diccionario. Para cada uno de ellos, haga lo siguiente.

  4. Convierta el tipo del valor de cada entrada a PrintProperty y utilícelo para crear un objeto PrintProperty.

  5. Obtenga el tipo de la propiedad Value de cada uno de los objetos 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();

Vea también

Referencia

PrintProperty

PrintSystemObject

System.Printing.IndexedProperties

PrintPropertyDictionary

LocalPrintServer

PrintQueue

DictionaryEntry

Conceptos

Documentos en WPF

Información general sobre impresión