Compartir a través de


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

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

Ejemplo

Los pasos para hacerlo son los siguientes.

  1. Crear una instancia del tipo. En el ejemplo siguiente, el tipo es el PrintQueue tipo que se incluye con Microsoft .NET Framework, pero el código casi idéntico debe funcionar para los tipos derivados de PrintSystemObject.

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

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

  4. Convierta el valor de cada entrada en PrintProperty y úselo para crear un PrintProperty objeto.

  5. Obtiene el tipo de Value de cada uno de los PrintProperty objetos.


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

Consulte también